C# 匯出excel按指定模板
阿新 • • 發佈:2019-02-16
private bool ExportForExecl(object sender, DoWorkEventArgs e)
{
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); //生成一個workbook物件
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得第一個 sheet
Object Nothing = System.Reflection.Missing.Value;
//公共屬性
int rowHeight = 15;
int columnWidth = 2;
int fontSize = 12;
Color color = Color.FromArgb(218, 150, 148);
int beginIndex = 3;//開始行
try
{
foreach (System.Data.DataTable dt in dts)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Range range = null;// 建立一個空的單元格物件
DataRow dr = dt.Rows[i];
string tableName = dr["表名"].ToString();
string tableMemo = dr["表說明"].ToString();
string sheetName = tableMemo.IsNotNullAndEmpty() ? tableMemo : tableName;
if (sheetName.IsNotNullAndEmpty())
{
//sheet名是否有重複,有重複就在名字後面加序數
if (sheetNames.Keys.Contains(sheetName))
{
int m = sheetNames[sheetName] + 1;
sheetName = sheetName + m.ToString();
sheetNames.Add(sheetName, m);
}
else
{
sheetNames.Add(sheetName, 1);
}
//第一個sheet不需要追加
if (i != 0)
{
sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Nothing, sheet, Nothing, Nothing);
}
beginIndex = 3;
sheet.Name = sheetName;
#region 首行
#region 第一行
//公共屬性
range = sheet.get_Range("C" + beginIndex, "BI" + beginIndex);// 獲取多個單元格
range.Columns.AutoFit(); // 設定列寬為自動適應
range.Borders.LineStyle = 1; // 設定單元格邊框
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設定單元格水平居中
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 設定單元格垂直居中
range.RowHeight = rowHeight; // 設定行高
range.ColumnWidth = columnWidth; // 設定列寬
range.Cells.Borders.LineStyle = 1;//設定全邊框
range.Font.Size = fontSize; // 設定字型大小
//表名列
range = sheet.get_Range("C" + beginIndex, "F" + beginIndex);// 獲取多個單元格
range.Merge(Missing.Value); // 合併單元格
range.Value2 = "表名";
range.Interior.Color = color; // 設定單元格背景色
#endregion
#region 第二行
beginIndex++;
//公共屬性
range = sheet.get_Range("C" + beginIndex, "BI" + beginIndex);// 獲取多個單元格
range.Columns.AutoFit(); // 設定列寬為自動適應
range.Borders.LineStyle = 1; // 設定單元格邊框
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設定單元格水平居中
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 設定單元格垂直居中
range.RowHeight = rowHeight; // 設定行高
range.ColumnWidth = columnWidth; // 設定列寬
range.Cells.Borders.LineStyle = 1;//設定全邊框
range.Font.Size = fontSize; // 設定字型大小
range.Interior.Color = color; // 設定單元格背景色
//NO
range = sheet.get_Range("C" + beginIndex, "D" + beginIndex);// 獲取多個單元格
range.Merge(Missing.Value); // 合併單元格
range.Value2 = "NO";
//列名
range = sheet.get_Range("E" + beginIndex, "N" + beginIndex);
range.Merge(Missing.Value);
range.Value2 = "列名";
//資料型別
range = sheet.get_Range("O" + beginIndex, "U" + beginIndex);
range.Merge(Missing.Value);
range.Value2 = "資料型別";
#endregion
#endregion
}
#region 資料行
beginIndex++;
//公共屬性
range = sheet.get_Range("C" + beginIndex, "BI" + beginIndex);// 獲取多個單元格
range.Columns.AutoFit(); // 設定列寬為自動適應
range.Borders.LineStyle = 1; // 設定單元格邊框
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設定單元格水平居中
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 設定單元格垂直居中
range.RowHeight = rowHeight; // 設定行高
range.ColumnWidth = columnWidth; // 設定列寬
range.Cells.Borders.LineStyle = 1;//設定全邊框
range.Font.Size = fontSize; // 設定字型大小
//NO
range = sheet.get_Range("C" + beginIndex, "D" + beginIndex);// 獲取多個單元格
range.Merge(Missing.Value); // 合併單元格
range.Value2 = dr["欄位序號"];
//列名
range = sheet.get_Range("E" + beginIndex, "N" + beginIndex);
range.Merge(Missing.Value);
range.Value2 = dr["欄位名"];
#endregion
//sheet.get_Range(sheet.Cells[29, 2], sheet.Cells[29, 2]).Orientation = Microsoft.Office.Interop.Excel.XlOrientation.xlVertical;//設定29行第2個單元格的字型豎直居中在單元格內
//range = sheet.get_Range("A1", Missing.Value);// 獲取單個單元格
//range.Font.Bold = true; // 加粗字型
// 如需列印檔案
//sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
}
excel.ActiveWorkbook.SaveAs(_ToPath, Nothing, Nothing, Nothing, Nothing, Nothing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
excel.ActiveWorkbook.Close(Nothing, Nothing, Nothing); //關閉Excel Work Book物件
excel.Quit(); //關閉Excel元件物件
GC.Collect();
MessageBox.Show("生成成功!");
System.Diagnostics.Process.Start(_ToPath);//自動開啟excel
// this.Close();//關閉程式
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.button3.Enabled = true;
return false;
}
return true;
}
{
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); //生成一個workbook物件
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得第一個 sheet
Object Nothing = System.Reflection.Missing.Value;
//公共屬性
int rowHeight = 15;
int columnWidth = 2;
int fontSize = 12;
Color color = Color.FromArgb(218, 150, 148);
int beginIndex = 3;//開始行
try
{
foreach (System.Data.DataTable dt in dts)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Range range = null;// 建立一個空的單元格物件
DataRow dr = dt.Rows[i];
string tableName = dr["表名"].ToString();
string tableMemo = dr["表說明"].ToString();
string sheetName = tableMemo.IsNotNullAndEmpty() ? tableMemo : tableName;
if (sheetName.IsNotNullAndEmpty())
{
//sheet名是否有重複,有重複就在名字後面加序數
if (sheetNames.Keys.Contains(sheetName))
{
int m = sheetNames[sheetName] + 1;
sheetName = sheetName + m.ToString();
sheetNames.Add(sheetName, m);
}
else
{
sheetNames.Add(sheetName, 1);
}
//第一個sheet不需要追加
if (i != 0)
{
sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Nothing, sheet, Nothing, Nothing);
}
beginIndex = 3;
sheet.Name = sheetName;
#region 首行
#region 第一行
//公共屬性
range = sheet.get_Range("C" + beginIndex, "BI" + beginIndex);// 獲取多個單元格
range.Columns.AutoFit(); // 設定列寬為自動適應
range.Borders.LineStyle = 1; // 設定單元格邊框
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設定單元格水平居中
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 設定單元格垂直居中
range.RowHeight = rowHeight; // 設定行高
range.ColumnWidth = columnWidth; // 設定列寬
range.Cells.Borders.LineStyle = 1;//設定全邊框
range.Font.Size = fontSize; // 設定字型大小
//表名列
range = sheet.get_Range("C" + beginIndex, "F" + beginIndex);// 獲取多個單元格
range.Merge(Missing.Value); // 合併單元格
range.Value2 = "表名";
range.Interior.Color = color; // 設定單元格背景色
#endregion
#region 第二行
beginIndex++;
//公共屬性
range = sheet.get_Range("C" + beginIndex, "BI" + beginIndex);// 獲取多個單元格
range.Columns.AutoFit(); // 設定列寬為自動適應
range.Borders.LineStyle = 1; // 設定單元格邊框
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設定單元格水平居中
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 設定單元格垂直居中
range.RowHeight = rowHeight; // 設定行高
range.ColumnWidth = columnWidth; // 設定列寬
range.Cells.Borders.LineStyle = 1;//設定全邊框
range.Font.Size = fontSize; // 設定字型大小
range.Interior.Color = color; // 設定單元格背景色
//NO
range = sheet.get_Range("C" + beginIndex, "D" + beginIndex);// 獲取多個單元格
range.Merge(Missing.Value); // 合併單元格
range.Value2 = "NO";
//列名
range = sheet.get_Range("E" + beginIndex, "N" + beginIndex);
range.Merge(Missing.Value);
range.Value2 = "列名";
//資料型別
range = sheet.get_Range("O" + beginIndex, "U" + beginIndex);
range.Merge(Missing.Value);
range.Value2 = "資料型別";
#endregion
#endregion
}
#region 資料行
beginIndex++;
//公共屬性
range = sheet.get_Range("C" + beginIndex, "BI" + beginIndex);// 獲取多個單元格
range.Columns.AutoFit(); // 設定列寬為自動適應
range.Borders.LineStyle = 1; // 設定單元格邊框
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設定單元格水平居中
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 設定單元格垂直居中
range.RowHeight = rowHeight; // 設定行高
range.ColumnWidth = columnWidth; // 設定列寬
range.Cells.Borders.LineStyle = 1;//設定全邊框
range.Font.Size = fontSize; // 設定字型大小
//NO
range = sheet.get_Range("C" + beginIndex, "D" + beginIndex);// 獲取多個單元格
range.Merge(Missing.Value); // 合併單元格
range.Value2 = dr["欄位序號"];
//列名
range = sheet.get_Range("E" + beginIndex, "N" + beginIndex);
range.Merge(Missing.Value);
range.Value2 = dr["欄位名"];
#endregion
//sheet.get_Range(sheet.Cells[29, 2], sheet.Cells[29, 2]).Orientation = Microsoft.Office.Interop.Excel.XlOrientation.xlVertical;//設定29行第2個單元格的字型豎直居中在單元格內
//range = sheet.get_Range("A1", Missing.Value);// 獲取單個單元格
//range.Font.Bold = true; // 加粗字型
// 如需列印檔案
//sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
}
excel.ActiveWorkbook.SaveAs(_ToPath, Nothing, Nothing, Nothing, Nothing, Nothing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
excel.ActiveWorkbook.Close(Nothing, Nothing, Nothing); //關閉Excel Work Book物件
excel.Quit(); //關閉Excel元件物件
GC.Collect();
MessageBox.Show("生成成功!");
System.Diagnostics.Process.Start(_ToPath);//自動開啟excel
// this.Close();//關閉程式
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.button3.Enabled = true;
return false;
}
return true;
}