首页 > 代码库 > 由DataSet导出生成excel的几种方法
由DataSet导出生成excel的几种方法
1、当dataset中包含了html等特殊字符用这个处理
public void CreateExcel2(DataSet ds) { //创建一个excel application Excel.Application xls_exp=null; int rowindex=1; int colindex=0; //创建一个workbook,一个worksheet Excel._Workbook xls_book=null; Excel._Worksheet xls_sheet=null; try { xls_exp = new Excel.ApplicationClass(); xls_book = xls_exp.Workbooks.Add(true); xls_sheet = (Excel._Worksheet)xls_book.ActiveSheet; //取得数据 //DataTable aa=GetData(); //将所得到的表的列名,赋值给单元格 foreach (DataColumn col in ds.Tables[0].Columns) { colindex++; xls_exp.Cells[1, colindex] = col.ColumnName; //水平对齐 xls_sheet.get_Range(xls_exp.Cells[1, colindex], xls_exp.Cells[1, colindex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; //垂直对齐 xls_sheet.get_Range(xls_exp.Cells[1, colindex], xls_exp.Cells[1, colindex]).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; //行高、列宽自适应 //xls_sheet.Cells.Rows.AutoFill(); //xls_sheet.Cells.Columns.AutoFill(); } //同样方法处理数据 foreach (DataRow row in ds.Tables[0].Rows) { rowindex++; colindex = 0; foreach (DataColumn col in ds.Tables[0].Columns) { colindex++; switch (row[col.ColumnName].GetType().ToString()) { //字符 case ("System.String"): //数字格式设置为文本 xls_sheet.get_Range(xls_exp.Cells[rowindex, colindex], xls_exp.Cells[rowindex, colindex]).NumberFormatLocal = "@"; //水平对齐 xls_sheet.get_Range(xls_exp.Cells[rowindex, colindex], xls_exp.Cells[rowindex, colindex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; //垂直对齐 xls_sheet.get_Range(xls_exp.Cells[rowindex, colindex], xls_exp.Cells[rowindex, colindex]).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; break; //日期 case ("System.DateTime"): //数字格式设置为yyyy-mm-dd hh:mm:ss日期 xls_sheet.get_Range(xls_exp.Cells[rowindex, colindex], xls_exp.Cells[rowindex, colindex]).NumberFormatLocal = "YYYY-MM-DD HH:MM:SS"; break; } //给cell赋值 xls_exp.Cells[rowindex, colindex] = row[col.ColumnName]; } } //不可见,即后台处理 xls_exp.Visible = true; } catch (Exception err) { //MessageBox.show(err.Message); Page.ClientScript.RegisterStartupScript(this.GetType(), "mes", "alert(‘"+err.Message+"‘);", true); } }
2.
public void CreateExcel(DataSet ds) { HttpResponse resp = null; resp = Page.Response; resp.Clear(); resp.Buffer = true; resp.Charset = "GB2312"; resp.ContentType = "application/ms-excel"; resp.AppendHeader("Content-Disposition", "attachment;filename=BleGgoods.xls" ); resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); this.EnableViewState = false; string colHeaders = "", ls_item = ""; //定义表对象与行对象,同时用DataSet对其值进行初始化 DataTable dt = ds.Tables[0]; DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的 int i = 0; int cl = dt.Columns.Count; //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符 for (i = 0; i < cl; i++) { if (i == (cl - 1))//最后一列,加\n { colHeaders += dt.Columns[i].Caption.ToString() + "\n"; } else { colHeaders += dt.Columns[i].Caption.ToString() + "\t"; } } resp.Write(colHeaders); //向HTTP输出流中写入取得的数据信息 //逐行处理数据 foreach (DataRow row in myRow) { //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 for (i = 0; i < cl; i++) { if (i == (cl - 1))//最后一列,加\n { ls_item += "‘"+row[i].ToString() + "\n"; } else { ls_item += "‘"+row[i].ToString() + "\t"; } } resp.Write(ls_item); ls_item = ""; } resp.Flush(); resp.End(); }
3.
public void CreateCsv(DataSet ds) { string output=string.Empty; ArrayList myOutput = new ArrayList(); if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { output = "\t" + ds.Tables[0].Rows[i][0].ToString() + "," + ds.Tables[0].Rows[i][1].ToString()+","; output += ds.Tables[0].Rows[i][2].ToString() + "," + ds.Tables[0].Rows[i][3].ToString()+","; output += ds.Tables[0].Rows[i][4].ToString() + "," + ds.Tables[0].Rows[i][5].ToString() + ","; output += ds.Tables[0].Rows[i][6].ToString() + "," + ds.Tables[0].Rows[i][7].ToString() + ","; output += ds.Tables[0].Rows[i][8].ToString(); myOutput.Add(output); } Response.Clear(); Response.ClearHeaders(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=BleGoods.csv"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文 Response.ContentType = "text/csv";//设置输出文件类型为text文件。 this.EnableViewState = false; System.Collections.IEnumerator myEnumerator = myOutput.GetEnumerator(); while (myEnumerator.MoveNext()) Response.Write(myEnumerator.Current.ToString()); Response.Flush(); Response.End(); } }
由DataSet导出生成excel的几种方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。