c# 汇出Excel

一:关于用到的类方法等

引用空间:Microsoft.Office.Interop.Excel

Microsoft.Office.Interop.Excel.Application:表示整个应用程序,new一个可以理解为打开一个Excel程序,当然汇出完的时候要退出该进程。

 Microsoft.Office.Interop.Excel.Workbook:表示应用程序内的单个工作薄

 Microsoft.Office.Interop.Excel.Worksheet:待补

Microsoft.Office.Interop.Excel.Range:常用单元格,用来处理样式

 

例:输出GV的列名

代码
public static void pub_excel_export(GridView gv)
{
Microsoft.Office.Interop.Excel.Application excel
= new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbooks workbooks
= excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook
= workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet
= (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

Microsoft.Office.Interop.Excel.Range range;

int int_cell_idx = 1;
for (int i = 0; i < gv.Columns.Count; i++)
{
worksheet.Cells[
1, int_cell_idx] = gv.Columns[i].HeaderText;
range
= (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, int_cell_idx];
range.Interior.ColorIndex
= 15;
range.Font.Bold
= true;
range.ColumnWidth
= 16;
range.RowHeight
= 25;
int_cell_idx
++;
}
//显示,没有处理退出进程问题
excel.Visible = true;
}

 

posted @ 2010-10-15 17:16  jorgen  阅读(671)  评论(0编辑  收藏  举报
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj