導出時:引用 using Excel = Microsoft.Office.Interop.Excel; #region 讀取excel //打開方法 public DataTable ExceltoDataSet(string path) { string strConn = "Provider= ...
導出時:引用 using Excel = Microsoft.Office.Interop.Excel;
#region 讀取excel
//打開方法
public DataTable ExceltoDataSet(string path)
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][2].ToString().Trim();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "Select * From [" + tableName + "]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, tableName);
System.Data.DataTable dt = ds.Tables[0];
return dt;
}
#endregion
#region 導出excel
/// <summary>
/// 將數據表保存到Excel表格中
/// </summary>
/// <param name="addr">Excel表格存放地址(程式運行目錄後面的部分)</param>
/// <param name="dt">要輸出的DataTable</param>
public void SaveToExcel(string addr, System.Data.DataTable dt, string sheetName)
{
//0.註意:
// * Excel中形如Cells[x][y]的寫法,前面的數字是列,後面的數字是行!
// * Excel中的行、列都是從1開始的,而不是0
//1.製作一個新的Excel文檔實例
Excel::Application xlsApp = new Excel::Application();
xlsApp.Workbooks.Add(true);
/* 示例輸入:需要註意Excel里數組以1為起始(而不是0)
* for (int i = 1; i < 10; i++)
* {
* for (int j = 1; j < 10; j++)
* {
* xlsApp.Cells[i][j] = "-";
* }
* }
*/
//2.設置Excel分頁卡標題
xlsApp.ActiveSheet.Name = sheetName;
//3.合併第一行的單元格
string temp = "";
if (dt.Columns.Count < 26)
{
temp = ((char)('A' + dt.Columns.Count)).ToString();
}
else if (dt.Columns.Count <= 26 + 26 * 26)
{
temp = ((char)('A' + (dt.Columns.Count - 26) / 26)).ToString()
+ ((char)('A' + (dt.Columns.Count - 26) % 26)).ToString();
}
else throw new Exception("列數過多");
Excel::Range range = xlsApp.get_Range("A1", temp + "1");
//range.ClearContents(); //清空要合併的區域
//range.MergeCells = true; //合併單元格
////4.填寫第一行:表名,對應DataTable的TableName
//xlsApp.Cells[1][1] = dt.TableName;
//xlsApp.Cells[1][1].Font.Name = "黑體";
//xlsApp.Cells[1][1].Font.Size = 25;
//xlsApp.Cells[1][1].Font.Bold = true;
//xlsApp.Cells[1][1].HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//居中
//xlsApp.Rows[1].RowHeight = 60; //第一行行高為60(單位:磅)
////5.合併第二行單元格,用於書寫表格生成日期
//range = xlsApp.get_Range("A2", temp + "2");
//range.ClearContents(); //清空要合併的區域
//range.MergeCells = true; //合併單元格
////6.填寫第二行:生成時間
//xlsApp.Cells[1][2] = "報表生成於:" + DateTime.Now.ToString();
//xlsApp.Cells[1][2].Font.Name = "宋體";
//xlsApp.Cells[1][2].Font.Size = 15;
////xlsApp.Cells[1][2].HorizontalAlignment = 4;//右對齊
//xlsApp.Cells[1][2].HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//居中
//xlsApp.Rows[2].RowHeight = 30; //第一行行高為60(單位:磅)
//7.填寫各列的標題行
xlsApp.Cells[1][1] = "ID";
for (int i = 0; i < dt.Columns.Count; i++)
{
xlsApp.Cells[i + 2][1] = dt.Columns[i].ColumnName;
}
xlsApp.Rows[1].Font.Name = "宋體";
xlsApp.Rows[1].Font.Size = 15;
xlsApp.Rows[1].Font.Bold = true;
//xlsApp.Rows[1].HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//居中
////設置顏色
//range = xlsApp.get_Range("A3", temp + "3");
//range.Interior.ColorIndex = 33;
//8.填寫DataTable中的數據
for (int i = 0; i < dt.Rows.Count; i++)
{
xlsApp.Cells[1][i + 2] = (i + 1).ToString();
for (int j = 0; j < dt.Columns.Count; j++)
{
xlsApp.Cells[j + 2][i + 2] = dt.Rows[i][j];
}
}
range = xlsApp.get_Range("A4", temp + (dt.Rows.Count + 1).ToString());
//range.Interior.ColorIndex = 37;
//range.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
////9.描繪邊框
//range = xlsApp.get_Range("A1", temp + (dt.Rows.Count + 1).ToString());
//range.Borders.LineStyle = 1;
//range.Borders.Weight = 3;
//10.打開製作完畢的表格
//xlsApp.Visible = true;
//11.保存表格到根目錄下指定名稱的文件中
xlsApp.ActiveWorkbook.SaveAs(addr);
xlsApp.Quit();
xlsApp = null;
GC.Collect();
}
#endregion