你的位置:首页 > ASP.net教程

[ASP.net教程]Winform 导入导出方法

导出时:引用 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