在ERP定制开发的过程中,很多操作都是和Excel相关的,主要就是学习一些Excel处理的方法, 从而熟悉且灵活使用它。
1) 使用DataReader循环读取Excel某列的数据:
- protected void Button1_Click(object sender, EventArgs e)
- {
- string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\WebSite8\App_Data\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; //定义excel的路径,可以使用完整文件名;
- DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); //使用他的原因就是对类进行抽象; 具体原理还不是很清楚,使用DbProviderFactory必须引用System.Data.Common;
- using (DbConnection connection = factory.CreateConnection()) //定义connection 对象
- {
- connection.ConnectionString = connectionString; //定义连接; 使用DBconnection的ConnectionString属性;
- using (DbCommand command = connection.CreateCommand()) //定义DbCommand对象;
- {
- // Cities$ comes from the name of the worksheet
- command.CommandText = "SELECT ID,City,State FROM [Cities$]"; //定义commandText;
- connection.Open();
- using (DbDataReader dr = command.ExecuteReader()) //定义DbDataReader对象, 通过dr.Read()属性判断结束,来遍历所有列的值;
- {
- while (dr.Read())
- {
- Response.Write(dr["ID"].ToString());
- //Response.Write(dr[2].ToString()); //当然也可以通过直接指定第几列来取具体的值;
- }
- }
- }
- }
- }
2) 以数据库访问的方式,把Excel的数据以放在gridview中显示:
- protected void Button2_Click(object sender, EventArgs e)
- {
- string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\WebSite8\App_Data\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
- try
- {
- string sql ="SELECT * FROM [Cities$]" ;
- OleDbConnection OleConn = new OleDbConnection(connectionString); //使用OleDbConnection需要引用"using System.Data.OleDb";
- OleConn.Open();
- OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn); //OleDataAdapter和SqlDataAdapter功能类似;
- DataSet OleDs = new DataSet();
- OleDaExcel.Fill(OleDs,"Cities");
- OleConn.Close();
- GridView1.DataSource = OleDs;
- GridView1.DataBind();
- }
- catch (Exception ex)
- {
- Response.Write("Fail to bind excel: "+ex.Message);
- }
- }
3) 把数据库里面的内容同步到Excel中: 使用Microsoft.Office.Interop.Excel.Application,里面有清楚的Workbook和Worksheet的定义:
- protected void Button3_Click(object sender, EventArgs e)
- {
- //添加组件,COM里面的:Microsoft Excel 11.0 Object Library
- Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
- System.Data.DataTable excelTable = new System.Data.DataTable();
- SqlConnection sqlconn = new SqlConnection("Data Source=efacsdb;Initial Catalog=whatifdb;User ID=sa; Password=L-BBgD7q");
- sqlconn.Open();
- SqlDataAdapter sda = new SqlDataAdapter("select top 10 * from t_tt_buyer_group", sqlconn);
- DataSet ds = new DataSet();
- sda.Fill(ds,"table1");
- excelTable = ds.Tables["table1"];
- string filePath ="c:\temp\test.xls";
- try
- {
- app.Visible = false;
- Workbook wb = app.Workbooks.Add(true);
- Worksheet ws = app.Worksheets[1] as Worksheet;
- if (excelTable.Rows.Count > 0)
- {
- int row = 0;
- row = excelTable.Rows.Count;
- int col = excelTable.Columns.Count;
- for (int i= 0; i<row; i++)
- {
- for (int j=0; j<col; j++)
- {
- string str = excelTable.Rows[i][j].ToString();
- ws.Cells[i+2,j+1] = str;
- }
- }
- }
- int size = excelTable.Columns.Count;
- for (int i=0;i< size;i++)
- {
- ws.Cells[1,1+i] = excelTable.Columns[i].ColumnName;
- }
- //不显示保存和覆盖的确认提示框:
- app.DisplayAlerts = false;
- app.AlertBeforeOverwriting = false;
- //保存工作簿
- wb.SaveCopyAs(@"C:\temp\test1.xls"); \\如果用wb.save()就会报错,提示Sheet1.xls无法访问; 保存时如果文件不存在也会报错;
- //保存excel文件
- //app.SaveWorkspace(filePath);
- app.Quit();
- app=null;
- }
- catch (Exception ex)
- {
- Response.Write("export error"+ex.Message);
- }
- }