⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 excellib.cs

📁 Cluster validation program
💻 CS
字号:
using System;
using System.Windows.Forms;

namespace DBLibrary
{
	/// <summary>
	/// 
	/// </summary>
	public class ExcelLib
	{
		public Excel.Application objExcel=null;

		public ExcelLib()
		{
		}

		public void ExcelInitial(){
			//  See if the Excel Application Object was successfully constructed
			try{
				if ( this.objExcel == null) {
					this.objExcel = new Excel.Application();
				}
			}
			catch( System.Exception e ){
				System.Windows.Forms.MessageBox.Show("ERROR: EXCEL couldn't be started!");
				System.Windows.Forms.Application.Exit();
			}
		}

		/// <summary>
		/// 货肺款 Excel拳老阑 父甸绊 ListView狼 郴侩阑 历厘茄促.
		/// </summary>
		public void SaveNewExcelData(System.String saveFileName, ListView listView){
			try{
				if (this.objExcel == null )
					return;
				Excel.Workbooks workbooks = this.objExcel.Workbooks;
				Excel._Workbook workbook = workbooks.Add( Excel.XlWBATemplate.xlWBATWorksheet );
				Excel.Sheets	sheets	= workbook.Worksheets;
				Excel._Worksheet worksheet = (Excel._Worksheet) sheets.get_Item(1);
				if (worksheet == null ){
					MessageBox.Show("Error: worksheet == null");
				}

				Excel.Range range1 = worksheet.get_Range("A1", "IV1");
				if (range1 == null){
					MessageBox.Show("Error: range == null");
				}


				// ListView ColumnHeader甫 Excel俊 持绰促.
				int columnHeader_Count = listView.Columns.Count; 
				for ( int j = 1; j <= columnHeader_Count ; j ++ ){
					range1.Cells.set_Item(1, j, listView.Columns[j - 1].Text );
				}

				// ListView狼 Item阑 Excel俊 持绰促
				int item_Count;
				ListViewItem item;
				int column_Count = listView.Items.Count;

				for (int i = 1; i <= listView.Items.Count ; i++){
					item = listView.Items[i - 1];
					item_Count = item.SubItems.Count;
					for ( int j = 1; j <= item.SubItems.Count ; j ++){
						range1.Cells.set_Item(i + 1, j,item.SubItems[j - 1].Text);
					}
				}

				// Excel 郴侩阑 拳老俊 历厘茄促
				workbook.Close(true, saveFileName, false);
				workbooks.Close();
			}
			catch( System.Exception e ){
				System.Windows.Forms.MessageBox.Show ( " 促澜苞 鞍篮 俊矾啊 惯积窍咯 历厘捞 角菩窍看嚼聪促.\n" + e.ToString());
				return;
			}

			// 历厘捞 场车促绰 皋技瘤甫 焊牢促
			System.Windows.Forms.MessageBox.Show( " 历厘窍看嚼聪促" );
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -