我们来自五湖四海,不为别的,只因有共同的爱好,为中国互联网发展出一分力!

C#导出Excel文件实例代码

2013年08月04日11:22 阅读: 13753 次
导出Excel的方法

///
</summary>

private void ExportExcel()

{

if
(this.table == null) return;

bool fileSaved =
false;

SaveFileDialog sfdSaveFile = new
SaveFileDialog();

sfdSaveFile.DefaultExt =
"xls";

sfdSaveFile.Filter =
"Excel文件(*.xls)|*.xls";

sfdSaveFile.FileName =
string.Empty;

if (sfdSaveFile.ShowDialog() != DialogResult.OK)
return;

//电脑Excel程序

Microsoft.Office.Interop.Excel.Application
xlApp = new Microsoft.Office.Interop.Excel.Application();

if (xlApp ==
null)

{

MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel程序!", "提示",
MessageBoxButtons.OK,
MessageBoxIcon.Error);

return;

}

//Workbook集合

Microsoft.Office.Interop.Excel.Workbooks
workbooks =
xlApp.Workbooks;

//Workbook

Microsoft.Office.Interop.Excel.Workbook
workbook =
workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

//WorkSheet

Microsoft.Office.Interop.Excel.Worksheet
worksheet =
(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1

//从DataTable中导出Excel

////写入字段列标题

//for
(int i = 0; i < this.table.Columns.Count;
i++)

//{

//    worksheet.Cells[1, i + 1] =
this.table.Columns.ColumnName;

//}

////写入数值

//for
(int r = 0; r < this.table.Rows.Count; r++)

//{

//    for
(int i = 0; i < this.table.Columns.Count;
i++)

//    {

//        worksheet.Cells[r + 2, i + 1] =
this.table.Rows[r]
;

//    }

//    System.Windows.Forms.Application.DoEvents();

//}

//从DataGridView中导出Excel

//标题

for
(int c = 0; c < this.dgvList.Columns.Count;
c++)

{

worksheet.Cells[1, c + 1] =
this.dgvList.Columns[c].HeaderText;

//设置单元格样式

Microsoft.Office.Interop.Excel.Range
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, c +
1];

range.Font.Name = "华文楷体";//字体

range.Font.Bold =
true;//是否加粗

range.Font.Italic = false;//是否倾斜

range.Font.Size =
15;//字体大小





//其他样式属性暂不知其中的值,不可用

//range.Font.Background
="#fff000";//背景颜色

//range.Font.Color
="#ff0000";//字颜色

//range.Font.ColorIndex =
3;

//range.Borders.Color =
"#00ffee";//边框颜色

}

//单元格内容

for (int rowIndex = 0;
rowIndex < this.dgvList.Rows.Count; rowIndex++)

{

for (int
columnIndex = 0; columnIndex < this.dgvList.Columns.Count;
columnIndex++)

{

worksheet.Cells[rowIndex + 2, columnIndex +
1] =
this.dgvList.Rows[rowIndex].Cells[columnIndex].Value.ToString();

}

}

worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。

//对指定列进行格式输出

//Microsoft.Office.Interop.Excel.Range
rg = worksheet.get_Range(worksheet.Cells[2, 1],
worksheet.Cells[this.table.Rows.Count + 1, 1]);

//rg.NumberFormat =
"00000000";

try

{

workbook.Saved =
true;

workbook.SaveCopyAs(sfdSaveFile.FileName);//保存复制到指定位置

fileSaved
= true;

}

catch (Exception ex)

{

fileSaved =
false;

MessageBox.Show("导出文件时出错,文件可能正被打开!\n" +
ex.Message);

}

finally

{

workbooks.Close();

xlApp.Quit();

GC.Collect();//强行销毁

}

if
(fileSaved &&
System.IO.File.Exists(sfdSaveFile.FileName))

{

//System.IO.File.Open(sfdSaveFile.FileName,
System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.Read);

System.Diagnostics.Process.Start(sfdSaveFile.FileName);
//打开EXCEL

}

}
分享到: 更多
蓝客门户
©2001-2017 中国蓝客联盟 版权所有.
关于蓝客联盟历史宗旨章程技术服务联系我们蓝客社区