解决方案 »

  1.   

    对于B/S app,非常不建议在服务器上调用COM操作excel/word文档,最好使用第三方组件。
    对于Excel的可选方案:简单无格式少数据量用OLEDB读写即可,其他情况建议用NOPI组件。
      

  2.   

    NOPI组件,之前用过没有成功,我再试试看。
      

  3.   

    npoi,aspose,epplus,openxml,spire.xls都是不依赖office组件的推荐使用
      

  4.   

    放弃Microsoft.Office.Interop.Excel的理由
    1.服务器上必须安装office,而且必须跟使用的dll版本一致
    2.32位64位必须一致
    3.需要注册DCOM
    4.需要给权限
    5.读取写入其实是利用excel进程打开新建,有时会有死进程,在单机版还可以杀死无窗口进程,在服务器多用户登陆多线程执行,根本没法处理
      

  5.   

    谢谢,各位,今天上午采用,NPOI 解决了EXCEL导出,就是表头无法添加边框,其他都可以添加边框,还有一个难题如何修改支持迅雷下载,我本想,生成的EXCEL文件保存到服务器上面(需要帮帮忙),然后再下载(这段代码我倒是写出来了),现在NPOI导出类这段代码不知怎么修改  #region 表头及样式
                    {
                        Row headerRow = sheet.CreateRow(0);
                        headerRow.HeightInPoints = 25;
                        headerRow.CreateCell(0).SetCellValue(strHeaderText);                    CellStyle headStyle = workbook.CreateCellStyle();                    //设置单元格边框 ,无法实现                        
                        headStyle.BorderBottom = CellBorderType.THIN;
                        headStyle.BorderLeft = CellBorderType.THIN;
                        headStyle.BorderRight = CellBorderType.THIN;
                        headStyle.BorderTop = CellBorderType.THIN;
                        headStyle.Alignment = HorizontalAlignment.CENTER;
                        Font font = workbook.CreateFont();
                        font.FontHeightInPoints = 20;
                        font.Boldweight = 700;
                        headStyle.SetFont(font);    /// <summary>
        /// 用于Web导出
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        /// <param name="strFileName">文件名</param>
        /// <Author> </Author>
        public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
        {        HttpContext curContext = HttpContext.Current;        // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition",
            "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));        curContext.Response.BinaryWrite(Export(dtSource, strHeaderText).GetBuffer());
            curContext.Response.End();    }
      

  6.   

    1.添加边框问题,因为表头是单独赋值的(使用列名,而不是值),所以你要把设置单元格边框的代码放在表头赋值那里一份
    你现在定义了headStyle但是没把他赋值给cell.Style啊,那不是白定义 了2.现在本来就是生成在服务器,然后用浏览器下载的
    如何支持迅雷下载是另一个问题,跟这个无关
      

  7.   

    ICell cell=headerRow.CreateCell(0);
    留下句柄,不要创建完就不要了,那你还怎么给它设置属性另外,我给你的链接你是一点都没看啊
    别人给你代码你不看,还在那问,其实都不太想继续搭理你了
      

  8.   

    下载的问题看DEMO 解决了。 现在 表头 边框还是有问题,看看怎么修改  。列头样式没问题 见代码                #region 表头及样式
                    {                    Row headerRow = sheet.CreateRow(0);
                        headerRow.HeightInPoints = 25;
                     
                        CellStyle headStyle = workbook.CreateCellStyle();
                        headStyle.Alignment = HorizontalAlignment.CENTER;                    Font font = workbook.CreateFont();
                        font.FontHeightInPoints = 20;
                        font.Boldweight = 700;
                        headStyle.SetFont(font);                    //设置单元格边框  在此无效                      
                        headStyle.BorderBottom = CellBorderType.THIN;
                        headStyle.BorderLeft = CellBorderType.THIN;
                        headStyle.BorderRight = CellBorderType.THIN;
                        headStyle.BorderTop = CellBorderType.THIN;
                        headerRow.CreateCell(0).SetCellValue(strHeaderText);
                        headerRow.GetCell(0).CellStyle = headStyle;
                                            Region rg = new Region(0, 0, 0, dtSource.Columns.Count - 1);
                        sheet.AddMergedRegion(Region.ConvertToCellRangeAddress(rg));
                        //headerRow.Dispose();
                    }
                    #endregion
                    #region 列头及样式
                    {
                        Row headerRow = sheet.CreateRow(1);
                        CellStyle headStyle = workbook.CreateCellStyle();
                        headStyle.Alignment = HorizontalAlignment.CENTER;
                        Font font = workbook.CreateFont();
                        font.FontHeightInPoints = 10;
                        font.Boldweight = 700;
                        headStyle.SetFont(font);                    //设置单元格边框                        
                        headStyle.BorderBottom = CellBorderType.THIN;
                        headStyle.BorderLeft = CellBorderType.THIN;
                        headStyle.BorderRight = CellBorderType.THIN;
                        headStyle.BorderTop = CellBorderType.THIN;
                        foreach (DataColumn column in dtSource.Columns)
                        {
                            headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                            headerRow.GetCell(column.Ordinal).CellStyle = headStyle;                       
                            //列头及样式设置列宽
                            sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
                        }
                        //headerRow.Dispose();
                    }
                    #endregion
      

  9.   

    服了你了
    你就不敢定义个变量
    ICell cell=headerRow.CreateCell()
    然后直接给cell.Style赋值
    非要先create,再get
    DEMO你真的看了吗
      

  10.   

    我觉得最好就是直接用XML创建一个Excel,然后流输出到客户端!!就能实现客户端"打开"、"保存"的功能!!
    NOPI没有试过!!