c#导出excel(转)
C#导出Excel文件实例代码
2010-08-03 14:10:36| 分类: 软件编程 | 标签:excel c#导出excel |字号大中小 订阅
/// <summary>
/// 导出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[i].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][i];
// }
// 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
}
}