c#操作EXCEL
/////////////////////以下是导出Excel的代码***************
Excel.Application excel;
int rowIndex=4;
int colIndex=1;
Excel._Workbook xBk;
Excel._Worksheet xSt;
excel= new Excel.ApplicationClass();;
xBk = excel.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.ActiveSheet;
//
//取得标题
//
foreach(DataColumn col in ds_Log.Tables[0].Columns)
{
colIndex++;
excel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
}
//
//取得表格中的数据
DataView dv = new DataView(ds_Log.Tables[0]);
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in ds_Log.Tables[0].Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
int rowSum = rowIndex ;//+ 1;
//自动换行?
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.WrapText=true;
//取得整个报表的标题
excel.Cells[2,2] = this.comboBox1.Text + "公司记录本号统计表";
//设置整个报表的标题格式
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 12;
//设置报表表格为最适应宽度
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//设置整个报表的标题为跨列居中
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
//绘制边框
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight =Excel.XlBorderWeight.xlThin;//设置左边线加粗
xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;//设置上边线加粗
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;//设置下边线加粗
//显示效果
excel.Visible=true;
excel.Quit();
Excel.Application excel = null;
Excel.Workbooks wbs = null;
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
try
{
excel = new Excel.Application();
catch(Exception ex)
{
}
finally
{
if (excel != null)
{
if (wbs != null)
{
if (wb != null)
{
if (ws != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
}
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect();
}
}
1) GC.Collect(); // this one forces garbage collection
2) System.Runtime.InteropServices.Marshal.ReleaseComObject (object); // this one releases the passed in COM object wrapper instance
The pattern the author (Peter A. Bromberg) uses to create and release COM Wrapped Excel objects is :-
1) call GC.Collect() to force collection of existing COM Objects waiting to be released.
2) instantiate the COM Wrapped Excel objects (Workbook, Worksheet etc.)
3) do the thing...
4) call the Close() or Quit() methods on the objects when done.
5) call System.Runtime.InteropServices.Marshal.ReleaseComObject(object) once for each COM object created.
6) set each object variable to null.
7) call GC.Collect() again
8) be relieved and reminisce the cool VB6 way of doing the above (Set obj = Nothing)
Here is a slightly altered & annotated code fragment (in C#) that shows how it is done :-
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
// Step 1
GC.Collect();// clean up any other excel guys hangin' around...
// Step 2
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
// Step 3
// this part will actually be filling in the values into the sheet
fillValues(oSheet);
....
// Step 4
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
// Step 5
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
// Step 6
oSheet=null;
oWB=null;
oXL = null;
// Step 7
GC.Collect(); // force final cleanup!
浙公网安备 33010602011771号