DataTable赋值
public static void DttoExcel_BCSYD(System.Data.DataTable dt, string InFolderPath)
//string InFolderPath = "";
//FolderBrowserDialog InFBDialog = new FolderBrowserDialog();
//if (InFBDialog.ShowDialog() == DialogResult.OK)
// InFolderPath = InFBDialog.SelectedPath;
string filePath = "";
if (dt == null)
return;
// 创建Excel应用程序对象
Microsoft.Office.Interop.Excel.Application excApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = excApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet workSeet = workBook.Worksheets[1]; //取得sheet1
Microsoft.Office.Interop.Excel.Range range = null;
int tableCount = dt.Rows.Count;
int rowRead = 0;
float percent = 0;
//添加表头
workSeet.get_Range("A1", "N1").Merge(workSeet.get_Range("A1", "N1").MergeCells);//合并单元格
workSeet.Cells[1, 1] = "表名";
filePath = InFolderPath + @"/" + "表名.xlsx";
Microsoft.Office.Interop.Excel.Range range_h = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[1, 1];
range_h.Font.Bold = true; //粗体
range_h.Font.Size = "21";//字体大小
range_h.Font.Name = "华文宋体";
range_h.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //居中
range_h.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
range_h.RowHeight = 23.25; //自动设置行高
//设置标题
workSeet.Cells[2, 1] = "A";
workSeet.Cells[2, 2] = "B";
workSeet.Cells[2, 3] = "C";
workSeet.Cells[2, 4] = "D";
workSeet.Cells[2, 5] = "E";
workSeet.Cells[2, 6] = "F";
workSeet.Cells[2, 7] = "G";
workSeet.Cells[2, 8] = "H";
workSeet.Cells[2, 9] = "I";
//设置标题的样式
//Microsoft.Office.Interop.Excel.Range range_1 = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[2, 1];
//Microsoft.Office.Interop.Excel.Range range_2 = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[3, 12];
//range = workSeet.get_Range(range_1, range_2);
range = workSeet.get_Range("A2:N2", Type.Missing);//使用对象的 Range 属性指定一个区域
range.Font.Bold = true; //粗体
range.Font.Size = "12";//字体大小
range.Font.Name = "华文宋体";
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //居中
range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
range.EntireColumn.AutoFit(); //自动设置列宽
range.EntireRow.AutoFit(); //自动设置行高
//for (int i = 0; i < dt.Columns.Count; i++) //数据库中的标题
// workSeet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
for (int s = 0; s < dt.Rows.Count; s++) // 给省赋值
workSeet.Cells[s + 3, 1] = " X市";
//设置样式
range = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[s + 3, 1];
range.Font.Size = 11; //字体大小
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //加边框
range.EntireColumn.AutoFit(); //自动调整列宽
for (int r = 0; r < dt.Rows.Count; r++)
for (int c = 0; c < dt.Columns.Count; c++)
//写入内容
workSeet.Cells[r + 3, c + 2] = "'" + dt.Rows[r][c].ToString();
//设置样式
range = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[r + 3, c + 2];
range.Font.Size = 11; //字体大小
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //加边框
range.EntireColumn.AutoFit(); //自动调整列宽
rowRead++;
percent = ((float)(100 * rowRead)) / tableCount;
System.Windows.Forms.Application.DoEvents();
int mc = 3;
int mi = 3;
for (int mr = 1; mr < 10; mr++)
Merge(workSeet, dt, mc, mr, mi); //合并值相同的Excel单元格
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
if (dt.Columns.Count > 1)
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
workBook.Saved = true;
workBook.SaveCopyAs(filePath);
catch { }
workBook.Close();
if (excApp != null)
excApp.Workbooks.Close();
excApp.Quit();
int generation = System.GC.GetGeneration(excApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excApp);
excApp = null;
System.GC.Collect(generation);
GC.Collect(); //强行销毁
MessageBox.Show("导出分析表格成功!");
OpenFile(filePath);
单数据赋值 可以加个参数list 吧要赋的值 传进来
public static void DttoExcel(string InFolderPath)
//string InFolderPath = "";
//FolderBrowserDialog InFBDialog = new FolderBrowserDialog();
//if (InFBDialog.ShowDialog() == DialogResult.OK)
// InFolderPath = InFBDialog.SelectedPath;
string filePath = "";
// 创建Excel应用程序对象
Microsoft.Office.Interop.Excel.Application excApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = excApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet workSeet = workBook.Worksheets[1]; //取得sheet1
Microsoft.Office.Interop.Excel.Range range = null;
//添加表头
workSeet.get_Range("A1", "H1").Merge(workSeet.get_Range("A1", "H1").MergeCells);//合并单元格
workSeet.Cells[1, 1] = "表名";
filePath = InFolderPath + @"/" + "表名.xlsx";
Microsoft.Office.Interop.Excel.Range range_h = (Microsoft.Office.Interop.Excel.Range)workSeet.Cells[1, 1];
range_h.Font.Bold = true; //粗体
range_h.Font.Size = "21";//字体大小
range_h.Font.Name = "华文宋体";
range_h.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //居中
range_h.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
range_h.RowHeight = 23.25; //自动设置行高
//设置标题
workSeet.get_Range("A2", "A3").Merge(workSeet.get_Range("A2", "A3").MergeCells);//合并单元格
workSeet.get_Range("B2", "B3").Merge(workSeet.get_Range("B2", "B3").MergeCells);
workSeet.get_Range("C2", "E2").Merge(workSeet.get_Range("C2", "E2").MergeCells);
workSeet.get_Range("F2", "H2").Merge(workSeet.get_Range("F2", "H2").MergeCells);
workSeet.Cells[2, 1] = "A";
workSeet.Cells[2, 2] = "B";
workSeet.Cells[2, 3] = "C";
workSeet.Cells[2, 6] = "D";
workSeet.Cells[3, 3] = "E";
workSeet.Cells[3, 4] = "F";
workSeet.Cells[3, 5] = "G";
workSeet.Cells[3, 6] = "H";
workSeet.Cells[3, 7] = "I";
workSeet.Cells[3, 8] = "J";
//设置标题的样式
range = workSeet.get_Range("A2:H3", Type.Missing);//使用对象的 Range 属性指定一个区域
range.Font.Bold = true; //粗体
range.Font.Size = "12";//字体大小
range.Font.Name = "华文宋体";
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //居中
range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
//range.EntireColumn.AutoFit(); //自动设置列宽
range.ColumnWidth = 17;//设置列宽
range.EntireRow.AutoFit(); //自动设置行高
//string endPosition = "A8";
workSeet.get_Range("A4", "A8").Merge(workSeet.get_Range("A4", "A8").MergeCells);//合并单元格
workSeet.Cells[4, 1] = "2021";
workSeet.Cells[4, 2] ="A";
workSeet.Cells[5, 2] = "B";
workSeet.Cells[6, 2] = "C";
workSeet.Cells[7, 2] = "D";
workSeet.Cells[8, 2] = "总计";
//设计内容样式
range = workSeet.get_Range("A4:H8", Type.Missing);//使用对象的 Range 属性指定一个区域
range.Font.Size = "9";//字体大小
range.Font.Name = "宋体";
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //居中
range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
range.EntireColumn.AutoFit(); //自动设置列宽
range.EntireRow.AutoFit(); //自动设置行高
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
workBook.Saved = true;
workBook.SaveCopyAs(filePath);
catch {
MessageBox.Show("保存分析表格失败!", "提示");
return;
workBook.Close();
if (excApp != null)
excApp.Workbooks.Close();
excApp.Quit();
int generation = System.GC.GetGeneration(excApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excApp);
excApp = null;
System.GC.Collect(generation);
GC.Collect(); //强行销毁
if (MessageBox.Show("导出分析表格成功!", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)