• 由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;
    
    
    //取得数据表各列标题,各标题之间以	分割,最后一个列标题后加回车符
    for (i = 0; i < cl; i++)
    {
    if (i == (cl - 1))//最后一列,加
    
    {
    colHeaders += dt.Columns[i].Caption.ToString() + "
    ";
    }
    else
    {
    colHeaders += dt.Columns[i].Caption.ToString() + "	";
    }
    
    }
    resp.Write(colHeaders);
    //向HTTP输出流中写入取得的数据信息
    
    //逐行处理数据
    foreach (DataRow row in myRow)
    {
    //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
    for (i = 0; i < cl; i++)
    {
    if (i == (cl - 1))//最后一列,加
    
    {
    ls_item += "'"+row[i].ToString() + "
    ";
    }
    else
    {
    ls_item += "'"+row[i].ToString() + "	";
    }
    
    }
    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 = "	" + 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();
    }
    
    }
    本博客有部分内容来自网络,如有问题请联系:hebeilijianghua@qq.com,并注明来自博客园。
  • 相关阅读:
    计算机科学导论第二周学习总结
    计算机科学概论2,3章小结
    2019—2020第一学期20202430王凯欣《网空概论》第四第五章学习小结
    2020—2021年第一学期20202430王凯欣《计算机科学概论》第一次笔记
    2020-2021第一学期20202410《计算机科学概论》第二次学习总结
    2020-2021第一学期20202410《计算机科学概论》第一次学习总结
    网页登录上网账号
    python从socket做个websocket的聊天室server
    windows下python 编码问题
    由css属性:vertial-align想到的。。
  • 原文地址:https://www.cnblogs.com/leebokeyuan/p/6626363.html
Copyright © 2020-2023  润新知