C# 导出生成Excel文件

#region 生成Excel文件
public static void ExportExcel(System.Data.DataTable dt, string strFile, string strMailCode)
{
LogTool lg = new LogTool();
lg.Write("获取DT记录数:" + dt.Rows.Count);
if (dt != null)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

lg.Write("打开Excel文件....");
if (excel == null) { return; }
excel.Visible = false;//设置为不可见,操作在后台执行,为 true 的话会打开 Excel

//excel.DisplayFullScreen = true;//打开时设置为全屏显式
Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;//初始化工作簿

//新增加一个工作簿,Add()方法也可以直接传入参数 true
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//同样是新增一个工作簿,但是会弹出保存对话框
//Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(true);

//新增加一个 Excel 表(sheet)
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
worksheet.Name = dt.TableName;//设置表的名称
try
{
Microsoft.Office.Interop.Excel.Range range;//创建一个单元格
int rowIndex = 1; //行的起始下标为 1
int colIndex = 1; //列的起始下标为 1

for (int i = 0; i < dt.Columns.Count; i++)//设置列名
{
//设置第一行,即列名
worksheet.Cells[rowIndex, colIndex + i] = dt.Columns[i].ColumnName;
//获取第一行的每个单元格
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[rowIndex, colIndex + i];
//设置单元格的内部颜色
range.Interior.ColorIndex = 0;//16//48//15
range.Font.Bold = false;//字体加粗
range.Font.Color = 0;//设置为黑色
range.Font.Name = "Calibri";//设置字体
range.Font.Size = 10;//设置字体大小
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//垂直居中
range.ColumnWidth = 12;//设置列宽
}

if (strMailCode == "103")
{
for (int y = 6; y < dt.Columns.Count; y++)
{
worksheet.Cells[rowIndex, colIndex + y] = dt.Columns[y].ColumnName;//设置第一行,即列名
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[rowIndex, colIndex + y];//获取第一行的每个单元格
range.Interior.ColorIndex = 17;//17//20 设置单元格的内部颜色
range.ColumnWidth = 24;//设置列宽
range.Font.Color = 2;
}
}

rowIndex++;//跳过第一行,第一行写入了列名

//写入数据
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
worksheet.Cells[rowIndex + i, colIndex + j] = dt.Rows[i][j].ToString();
}
}
//******************************************************************************************
worksheet.Rows.RowHeight = 16;
worksheet.Rows.Font.Size = 10;
worksheet.Rows.Font.Name = "Calibri";
worksheet.Rows.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).RowHeight = 24;// 表头行高

//******************************************************************************************
if (strMailCode == "101")
{
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[dt.Rows.Count + 1, 3]).ColumnWidth = 9;
worksheet.get_Range(worksheet.Cells[1, 4], worksheet.Cells[dt.Rows.Count + 1, 4]).ColumnWidth = 10;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 4], worksheet.Cells[dt.Rows.Count + 1, 4]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[dt.Rows.Count + 1, 12]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 13], worksheet.Cells[dt.Rows.Count + 1, 13]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 14], worksheet.Cells[dt.Rows.Count + 1, 14]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
for (int y = 6; y <= 11; y++)
{
worksheet.get_Range(worksheet.Cells[1, y], worksheet.Cells[dt.Rows.Count + 1, y]).ColumnWidth = 8;
}
worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[dt.Rows.Count + 1, 12]).ColumnWidth = 18;
worksheet.get_Range(worksheet.Cells[1, 13], worksheet.Cells[dt.Rows.Count + 1, 13]).ColumnWidth = 18;
worksheet.get_Range(worksheet.Cells[1, 14], worksheet.Cells[dt.Rows.Count + 1, 14]).ColumnWidth = 18;
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).Font.Bold = true;
}

if (strMailCode == "102")
{
for (int y = 4; y <= dt.Columns.Count; y++)
{
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, y];//获取第一行的每个单元格
range.Interior.ColorIndex = 15;//17//20 设置单元格的内部颜色
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平居中
}

lg.Write("Excel第一列合并单元格....");
string strBeginArea = "";
for (int i = 1; i < dt.Rows.Count + 1; i++)
{
string temp = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[i, 1]).Text.ToString().Trim();
if (strBeginArea == temp)
{
Microsoft.Office.Interop.Excel.Range colMer = worksheet.get_Range(worksheet.Cells[i - 1, 1], worksheet.Cells[i, 1]);
colMer.Application.DisplayAlerts = false;
colMer.Merge(System.Type.Missing);
colMer.Application.DisplayAlerts = true;
}
strBeginArea = temp;
}
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dt.Rows.Count + 1, 1]).ColumnWidth = 10;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).ColumnWidth = 18;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).Font.Bold = true;
}
if (strMailCode == "104")
{
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows["1:1", System.Type.Missing]).Font.Bold = true;
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 1, 1]).NumberFormatLocal = "yyyy-mm";
worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).NumberFormatLocal = "000000";
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dt.Rows.Count + 1, 1]).ColumnWidth = 8;
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[dt.Rows.Count + 1, 2]).ColumnWidth = 8;
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[dt.Rows.Count + 1, 3]).ColumnWidth = 32;
worksheet.get_Range(worksheet.Cells[1, 8], worksheet.Cells[dt.Rows.Count + 1, 8]).ColumnWidth = 6;
worksheet.get_Range(worksheet.Cells[1, 13], worksheet.Cells[dt.Rows.Count + 1, 13]).ColumnWidth = 9;
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[dt.Rows.Count + 1, 3]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 5], worksheet.Cells[dt.Rows.Count + 1, 5]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 7], worksheet.Cells[dt.Rows.Count + 1, 7]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;//水平居右
worksheet.get_Range(worksheet.Cells[1, 9], worksheet.Cells[dt.Rows.Count + 1, 9]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 10], worksheet.Cells[dt.Rows.Count + 1, 10]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 11], worksheet.Cells[dt.Rows.Count + 1, 11]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[dt.Rows.Count + 1, 12]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//水平居左
//Microsoft.Office.Interop.Excel.Range colMer = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 1, 1]);
//colMer.NumberFormatLocal = "yyyy-mm";
}

//是否提示,如果想删除某个sheet页,首先要将此项设为fasle。
excel.DisplayAlerts = false;
workbook.Saved = true;//保存写入的数据,这里还没有保存到磁盘
//创建文件
FileStream file = new FileStream(strFile, FileMode.CreateNew);

//关闭释放流,不然没办法写入数据
file.Close();
file.Dispose();

//保存到指定的路径
workbook.SaveCopyAs(strFile);
}
catch (Exception err)
{
lg.Write("\n出错了:" + err.Message);
}
finally
{
workbook.Close(false, Type.Missing, Type.Missing);
workbooks.Close();
excel.Quit();//关闭退出

worksheet = null;
workbook = null;
workbooks = null;
excel = null;

GC.Collect();
}
}
}
#endregion

猜你喜欢

转载自www.cnblogs.com/812FC/p/12167602.html