?? excellib.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 + -