通过Workbook类 生成Excel导出数据

需求:

     实现错误信息生成Excel保存到本地让用户查看。

刚开始使用了微软自带的Microsoft.Office.Interop.Excel类库、

 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                excel.Visible = true;
                //新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错  
                excel.Application.Workbooks.Add(true);
                //生成Excel中列头名称  
                excel.Cells[1, 1] = "姓名";
                excel.Cells[1, 2] = "工号";
                excel.Cells[1, 3] = "公司";
                excel.Cells[1, 4] = "部门";
                excel.Cells[1, 5] = "失败原因";

                //把newdt当前页的数据保存在Excel中  
                for (int i = 0; i < newdt.Rows.Count; i++)
                {
                    excel.Cells[i + 2, 1] = "'" + newdt.Rows[i]["Emp_Name"];
                    excel.Cells[i + 2, 2] = "'" + newdt.Rows[i]["Emp_Code"];
                    excel.Cells[i + 2, 3] = "'" + newdt.Rows[i]["CompanyName"];
                    excel.Cells[i + 2, 4] = "'" + newdt.Rows[i]["DeptName"];
                    excel.Cells[i + 2, 5] = "'" + newdt.Rows[i]["Error"];
                }
                //设置禁止弹出保存和覆盖的询问提示框  
                excel.DisplayAlerts = false;
                excel.AlertBeforeOverwriting = false;
                //保存工作簿  
                excel.Application.Workbooks.Add(true).Save();
                //保存excel文件  
                excel.Save("D:" + "\\错误信息.xls");
                //确保Excel进程关闭  
                excel.Quit();
                excel = null;

虽然功能实现了、但是由于服务器上没有安装Excel、无法调用COM+组件。

并且COM+组件  听说如果导出过程中出问题可能导致服务器宕机。


经过查阅,修改为调用Aspose.Cells组件。

        /// <summary>
        /// 导出数据到本地
        /// </summary>
        /// <param name="dt">要导出的数据</param>
        /// <param name="Name">标题</param>
        /// <param name="path">保存路径</param>
        public void OutFileToDisk(DataTable dt, string Name, string path)
        {
            Workbook workbook = new Workbook();
            /* 工作簿 */
            Worksheet sheet = workbook.Worksheets[0];                               /* 工作表 */
            Cells cells = sheet.Cells;                                          /* 单元格 //为标题设置样式 */
            Style styleTitle = workbook.Styles[workbook.Styles.Add()];               /* 新增样式 */
            Style style2 = workbook.Styles[workbook.Styles.Add()];                                  /* 新增样式 */
            style2.HorizontalAlignment = TextAlignmentType.Center;     /* 文字居中 */
            style2.Font.Name = "宋体";                         /* 文字字体 */
            style2.Font.Size = 12;                           /* 文字大小 */
            style2.Font.IsBold = true;                         /* 粗体 */
            style2.IsTextWrapped = true;                         /* 单元格内容自动换行 */
            style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;          /* 样式3 */
            Style style3 = workbook.Styles[workbook.Styles.Add()];                                  /* 新增样式 */
            style3.HorizontalAlignment = TextAlignmentType.Center;     /* 文字居中 */
            style3.Font.Name = "宋体";                         /* 文字字体 */
            style3.Font.Size = 10;                           /* 文字大小 */
            style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            int Colnum = dt.Columns.Count;     /* 表格列数 */
            int Rownum = dt.Rows.Count;
            /* 生成行2 列名行 */
            for (int i = 0; i < Colnum; i++)
            {
                cells[0, i].PutValue(dt.Columns[i].ColumnName);
                cells[0, i].SetStyle(style2);
                cells.SetRowHeight(0, 20);
                cells.SetColumnWidth(i, 20);
            }
            /* 生成数据行 */
            for (int i = 0; i < Rownum; i++)
            {
                for (int k = 0; k < Colnum; k++)
                {
                    cells[1 + i, k].PutValue(dt.Rows[i][k].ToString());
                    cells[1 + i, k].SetStyle(style3);
                }
                cells.SetRowHeight(1 + i, 18);
            }
            //workbook.Save(path);//可以通过Save直接存储
            MemoryStream ms = workbook.SaveToStream();
            byte[] bt = ms.ToArray();
            string fileName = Name + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";//客户端保存的文件名
            //以字符流的形式下载文件   
            Response.ContentType = "application/vnd.ms-excel";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bt);
            Response.Flush();
            Response.End();
        }

分享供大家学习、

posted @ 2015-03-27 12:16  知识青年下乡去  阅读(7938)  评论(3编辑  收藏  举报