第六章 Excel报表开发
1.excel导入到数据库 (ADO)
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";Data Source=" + path); conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet4$]", conn); //填充 DataSet ds = new DataSet(); da.Fill(ds); string sql = string.Empty; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sql += string.Format("insert into ExcelTable values('{0}','{1}','{2}')", ds.Tables[0].Rows[i][0].ToString(), ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][2].ToString()); } int rowCount = sqlhlper.runsql(sql); State = "ok";
2.数据库中的数据导出到excel (服务器)
(1)引入头文件 :using Excel=Micosoft.office.Interop.Excel;
(2)创建应用程序对象
(3)创建workbook对象
(4)创建worksheet对象
(5)操作每一行每一列的单元格
(6)保存
(7)退出程序
例如:
//创建应用程序 Excel.Application app = new Excel.Application(); //创建workbook对象 Excel.Workbook workbook = app.Workbooks.Add(true); //创建worksheet对象 Excel.Worksheet worksheet = workbook.Worksheets[1]; worksheet.Name = "sheet1"; //操作单元格 //操作列 for (int col = 0; col < ds .Tables [0].Columns .Count ; col++) { worksheet.Cells[1, col + 1] = ds.Tables[0].Columns[col].ColumnName; } //操作行 for (int i = 0; i < ds .Tables [0].Rows .Count ; i++) { for (int j = 0; j < ds .Tables [0].Columns .Count ; j++) { worksheet.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString(); } } //保存(服务器) workbook.SaveAs(Server.MapPath(@"~/studens.xls")); //退出 app.Workbooks.Close(); app.Quit(); app = null;
3.excel导入Gridview
(1)正常
string path =Server .MapPath("~/GFbook.xls"); OleDbConnection conn =new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;\";Data Source="+path); conn.Open(); //读取数据 OleDbDataAdapter adp = new OleDbDataAdapter("select * from [sheet1$]", conn); //将数据填充到dataset中 DataSet ds = new DataSet(); adp.Fill(ds); //关闭 conn.Close(); //释放 adp.Dispose(); //将数据绑定到gridview上 gvExcel.DataSource = ds; gvExcel.DataBind();
(2)混排(HDR = YES;IMEX=1)
//前八行混合形式 string path = Server.MapPath("~/GFbook.xls"); OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";Data Source=" + path); conn.Open(); //读取数据 OleDbDataAdapter adp = new OleDbDataAdapter("select * from [sheet2$]", conn); //将数据填充到dataset中 DataSet ds = new DataSet(); adp.Fill(ds); //关闭 conn.Close(); //释放 adp.Dispose(); //将数据绑定到gridview上 gvExcel.DataSource = ds; gvExcel.DataBind();
(3)前8排 (设置TypeGuessRows=0)
string path = Server.MapPath("~/GFbook.xls"); OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";Data Source=" + path); conn.Open(); //读取数据 OleDbDataAdapter adp = new OleDbDataAdapter("select * from [sheet3$]", conn); //将数据填充到dataset中 DataSet ds = new DataSet(); adp.Fill(ds); //关闭 conn.Close(); //释放 adp.Dispose(); //将数据绑定到gridview上 gvExcel.DataSource = ds; gvExcel.DataBind();
4.数据库中的数据导出到客户端(客户端)
(1)Aspose.cells组件:
引入using Aspose .excel;
(2)创建workbook工作薄
workbook wb =new workbook();
(3)创建worksheet对象
worksheet ws=wb.worksheets.add("xxx");
(4)操作奖励单元格
Cells cells =ws.Cells;
cells[A1].Putvalue("value");
(5)保存对象
SaveOptions so = wb.SaveOptions ;
so .SaveFormart = SaveFormart.auto;//默认的类型
wb.save(Response,"文件名",ContentDispostion.Attachment,so);
5.office api(excel)
6.2003版本的连接字符串:
OleDbConnection conn =new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=\"Excel 8.0;\";Data Source="+path);
7.2007版本的连接字符串:
OleDbConnection conn =new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=\"Excel 12.0;\";Data Source="+path);
8.添加头文件: using system.Data.oleDB;
9.当前程序路径:
(1)所在路径: system.AppDomain.CurrentDomain.BaseDireCtory;
(2)文件执行路径:system .Environment.CurrentDdirectory;