• 不引用office动态库导出excel


    public class OutExcelReport
    {
    /// <summary>
    /// 把 DataSet 的数据导成 Excel
    /// </summary>
    /// <param name="p_dsExport">要导出的 DataSet</param>
    /// <param name="p_strFileName">下载时客户端默认的文件名</param>
    /// <param name="p_blnHaveHeaderText">true 标示第一行是列名,默认值为 true</param>
    /// <param name="rrModel">报表统计结果</param>
    /// <param name="formType">报表物资类型</param>
    public static void DataSetToExcel(DataSet p_dsExport, string p_strFileName, bool p_blnHaveHeaderText, ReportResultModel rrModel, string formType)
    {
    if (p_dsExport == null)
    {
    return;
    }
    string strContext = GenerateWorkSheet(p_dsExport, rrModel, formType);

    DownloadExcelFile(strContext, p_strFileName);
    }


    /// <summary>
    ///
    /// </summary>
    /// <param name="p_strFileContext"></param>
    /// <param name="p_strFileName"></param>
    public static void DownloadExcelFile(string p_strFileContext, string p_strFileName)
    {
    // Appending Headers
    if (CommonFunc.IsNullString(p_strFileName))
    {
    p_strFileName = "Excel.xls";
    }

    if (!p_strFileName.Trim().ToLower().EndsWith(".xls"))
    {
    p_strFileName += ".xls";
    }

    try
    {
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Buffer = true;
    p_strFileName = CommonFunc.UrlEncode(p_strFileName);
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + p_strFileName);
    }
    catch
    {
    }

    //Writeout the Content
    HttpContext.Current.Response.Write(p_strFileContext);
    try
    {
    HttpContext.Current.Response.End();
    }
    catch
    {
    }
    }

    public static string GenerateWorkSheetBottom(string formType, ReportResultModel rrModel)
    {
    string[] strArraw = new string[8];
    switch (formType.ToLower())
    {
    case "dormrepair": //宿舍维修申请
    {
    strArraw = new string[] { "统计项", rrModel.Total, "运行中申请单数", rrModel.Running, "已完成申请单数", rrModel.Finished, "", ""};
    break;
    }
    case "managerepair": //办公维修
    {
    strArraw = new string[] { "统计项", rrModel.Total, "运行中申请单数", rrModel.Running, "已完成申请单数", rrModel.Finished, "", "" };
    break;
    }
    case "winecollar": //酒水申请
    {
    strArraw = new string[] { "统计项", rrModel.Total, "运行中申请单数", rrModel.Running, "已完成申请单数", rrModel.Finished, "酒水申请成本汇总", rrModel.AllCost };
    break;
    }
    case "publicactivitysupply"://纪念品
    {
    strArraw = new string[] { "统计项", rrModel.Total, "运行中申请单数", rrModel.Running, "已完成申请单数", rrModel.Finished, "纪念品申请成本汇总", rrModel.AllCost };
    break;
    }
    case "adminmaterial": //行政物资
    {
    strArraw = new string[] { "统计项", rrModel.Total, "运行中申请单数", rrModel.Running, "已完成申请单数", rrModel.Finished, "行政物资成本汇总", rrModel.AllCost };
    break;
    }
    case "consumables": //办公用品
    {
    strArraw = new string[] { "统计项", rrModel.Total, "运行中申请单数", rrModel.Running, "已完成申请单数", rrModel.Finished, "办公文具成本汇总", rrModel.AllCost };
    break;
    }
    case "receptroom": //接待房
    {
    strArraw = new string[] { "统计项", rrModel.Total, "运行中申请单数", rrModel.Running, "已完成申请单数", rrModel.Finished, "接待房成本汇总", rrModel.AllCost };
    break;
    }
    default:
    break;
    }

    StringBuilder strExcelXml = new StringBuilder();
    strExcelXml.Append("<Row></Row> <Row>");
    for (int j = 1; j < 9; j++)
    {
    strExcelXml.Append("<Cell ss:Index="" + (j).ToString() + ""><Data ss:Type="String">");
    strExcelXml.Append(CommonFunc.HtmllEncode(strArraw[j - 1]));
    strExcelXml.Append("</Data></Cell> ");
    }
    strExcelXml.Append("</Row> ");
    return strExcelXml.ToString();

    }

    public static string GenerateWorkSheet(DataSet p_dsExport, ReportResultModel rrModel, string formType)
    {
    StringBuilder strExcelXml = new StringBuilder();
    strExcelXml.Append(ExcelHeader());
    strExcelXml.Append(ExcelWorkSheetOptions());

    foreach (DataTable dt in p_dsExport.Tables)
    {
    strExcelXml.Append("<Worksheet ss:Name="" + dt.TableName + "">");
    strExcelXml.Append("<Table>");
    #region 用车报表导出的表头有两行
    //added by dengqian 2013-09-22
    if (formType.Equals("manageinfocar") || formType.Equals("industrycar"))
    {
    strExcelXml.Append(GetHeaderText_tworow(dt));
    }
    else
    {
    strExcelXml.Append(GetHeaderText(dt));
    }
    #endregion
    //old: strExcelXml.Append(GetHeaderText(dt));

    int intColCount = dt.Columns.Count;
    foreach (DataRow dr in dt.Rows)
    {
    strExcelXml.Append("<Row> ");
    for (int j = 0; j < intColCount; j++)
    {
    strExcelXml.Append("<Cell ss:Index="" + (j + 1).ToString() + ""><Data ss:Type="String">");
    strExcelXml.Append(CommonFunc.HtmllEncode(CommonFunc.ObjectToNullStr(dr[j])));
    strExcelXml.Append("</Data></Cell> ");
    }
    strExcelXml.Append("</Row> ");
    }
    if (rrModel != null)
    {
    strExcelXml.Append(GenerateWorkSheetBottom(formType, rrModel));
    }
    strExcelXml.Append("</Table>");
    strExcelXml.Append("</Worksheet>");
    }
    strExcelXml.Append("</Workbook> ");
    return strExcelXml.ToString();
    }


    private static string GetHeaderText(DataTable p_dt)
    {
    StringBuilder sbRtn = new StringBuilder();
    sbRtn.Append("<Row>");
    foreach (DataColumn dc in p_dt.Columns)
    {
    sbRtn.Append("<Cell><Data ss:Type="String">");
    sbRtn.Append(dc.ColumnName);
    sbRtn.Append("</Data></Cell> ");
    }
    sbRtn.Append("</Row>");
    return sbRtn.ToString();
    }

    #region 用车报表 两行表头的
    //added dengqian 201309016
    private static string GetHeaderText_tworow(DataTable p_dt)
    {
    StringBuilder sbRtn = new StringBuilder();
    StringBuilder partRtn = new StringBuilder();
    sbRtn.Append("<Row>");

    partRtn.Append("<Row>");
    int rownum = 0;
    foreach (DataColumn dc in p_dt.Columns)
    {
    if (rownum == 0)
    {
    sbRtn.Append("<Cell rowSpan='2'><Data ss:Type="String">");
    sbRtn.Append(dc.ColumnName);
    sbRtn.Append("</Data></Cell> ");
    partRtn.Append("<Cell colSpan='3'><Data ss:Type="String"></Data></Cell> ");
    }
    else if (rownum > 0 && rownum <4)
    {
    if (rownum == 1)
    {
    sbRtn.Append("<Cell colSpan='3'><Data ss:Type="String">短途用车(趟)</Data></Cell> ");
    }
    else
    {
    sbRtn.Append("<Cell colSpan='3'><Data ss:Type="String"></Data></Cell> ");
    }
    partRtn.Append("<Cell><Data ss:Type="String">");
    partRtn.Append(dc.ColumnName);
    partRtn.Append("</Data></Cell> ");
    //rownum += 3;
    }
    else if (rownum >3 && rownum <11)
    {
    if (rownum == 4)
    {
    sbRtn.Append("<Cell colSpan='7'><Data ss:Type="String">长途用车(趟)</Data></Cell> ");
    }
    else
    {
    sbRtn.Append("<Cell colSpan='3'><Data ss:Type="String"></Data></Cell> ");
    }
    partRtn.Append("<Cell><Data ss:Type="String">");
    partRtn.Append(dc.ColumnName);
    partRtn.Append("</Data></Cell> ");
    //rownum += 7;
    }
    else
    {
    sbRtn.Append("<Cell rowSpan='2'><Data ss:Type="String">");
    sbRtn.Append(dc.ColumnName);
    sbRtn.Append("</Data></Cell> ");
    }
    rownum++;
    }
    sbRtn.Append("</Row>");
    partRtn.Append("</Row>");
    sbRtn.Append(partRtn);
    return sbRtn.ToString();
    }

    #endregion

    /// <summary>
    /// Creates Excel Header
    /// </summary>
    /// <returns>Excel Header Strings</returns>
    private static string ExcelHeader()
    {
    // Excel header
    StringBuilder sb = new StringBuilder();
    sb.Append("<?xml version="1.0"?> ");
    sb.Append("<?mso-application progid="Excel.Sheet"?> ");
    sb.Append("<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" ");
    sb.Append("xmlns:o="urn:schemas-microsoft-com:office:office" ");
    sb.Append("xmlns:x="urn:schemas-microsoft-com:office:excel" ");
    sb.Append("xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ");
    sb.Append("xmlns:html="http://www.w3.org/TR/REC-html40"> ");
    sb.Append("<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">");
    sb.Append("<Author></Author>");
    sb.Append("</DocumentProperties>");
    sb.Append("<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> ");
    sb.Append("<ProtectStructure>False</ProtectStructure> ");
    sb.Append("<ProtectWindows>False</ProtectWindows> ");
    sb.Append("</ExcelWorkbook> ");

    return sb.ToString();
    }

    private static string ExcelWorkSheetOptions()
    {
    // This is Required Only Once , But this has to go after the First Worksheet's First Table
    StringBuilder sb = new StringBuilder();
    sb.Append(" <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <Selected/> </WorksheetOptions> ");
    return sb.ToString();
    }
    }

    导出方法示例

    DataSet ds=....;
    OutExcelReport.DataSetToExcel(ds, filename, false, null, "");

  • 相关阅读:
    搭建es7.5的配置文件
    kafka的暂停消费和重新开始消费问题
    hive sparksession查询只显示defalt库问题
    flink widow&window funcion&水印
    flink支持的数据类型讲解(可序列化) 和 内置累加器的运用
    mysql tar安装模式
    Permission denied: user=root, access=WRITE, inode="/":hdfs:supergroup:drwxr-xr-x
    错误Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FSDataInputStream排查思路
    SPSS非参数检验
    SPSS回归分析
  • 原文地址:https://www.cnblogs.com/lanyue52011/p/3372485.html
Copyright © 2020-2023  润新知