Aspose.Cells生成Excel 服务器不用安装office

生成Excel并在浏览器中下载的方法:

        /// 
        /// 导出
        /// 
        /// 数据DataTable
        /// 文件名
        /// 响应请求
        public static void Export(DataTable dt, string sheetName, HttpResponseBase Response)
        {
            Workbook workbook = new Workbook();//工作簿 
            Worksheet sheet = (Worksheet)workbook.Worksheets[0];//工作表 
            sheet.Name = sheetName;

            Cells cells = sheet.Cells;//单元格 

            //为标题设置样式     
            Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 
            styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 
            styleTitle.Font.Name = "宋体";//文字字体 
            styleTitle.Font.Size = 15;//文字大小 
            styleTitle.Font.IsBold = true;//粗体

            //样式2 
            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.Left;//文字居中 
            style3.Font.Name = "宋体";//文字字体 
            style3.Font.Size = 11;//文字大小 
            style3.IsTextWrapped = true;//单元格内容自动换行
            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;//表格行数 

            //生成行1 标题行    
            cells.Merge(0, 0, 1, Colnum);//合并单元格 
            cells[0, 0].PutValue(sheetName);//填写内容 
            cells[0, 0].SetStyle(styleTitle);
            cells.SetRowHeight(0, 35);

            //生成行2 列名行 
            for (int i = 0; i < Colnum; i++)
            {
                cells[1, i].PutValue(dt.Columns[i].ColumnName);
                cells[1, i].SetStyle(style2);
                cells.SetRowHeight(1, 25);//设置行高

                //设置列宽度
                //cells.SetColumnWidth(i, 30);
            }

            //生成数据行 
            for (int i = 0; i < Rownum; i++)
            {
                for (int k = 0; k < Colnum; k++)
                {
                    cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());
                    cells[2 + i, k].SetStyle(style3);
                }
                cells.SetRowHeight(2 + i, 20);
            }
            sheet.AutoFitColumns();//自动适应所有列宽
            //sheet.AutoFitRows();//自动适应所有行高

            //下载Excel文件
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "utf-8";
            string filename = HttpUtility.UrlEncode(DateTime.Now.ToString(sheetName));
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            Response.BinaryWrite(workbook.SaveToStream().ToArray());
            Response.End();
            Response.Close();
        }

创建表格并调用生成Excel下载

List activityList = ActivityResult.Data;

            if (activityList.Count>0)
            {
                //新增列头
                DataTable dt = new DataTable();
                dt.Columns.Add("活动名称");
                dt.Columns.Add("总浏览量");
                dt.Columns.Add("浏览量(安卓)");
                dt.Columns.Add("浏览量(苹果)");
                dt.Columns.Add("分享总次数");
                dt.Columns.Add("分享次数(安卓)");
                dt.Columns.Add("分享次数(苹果)");
                dt.Columns.Add("参与人数");

                //新增内容
                foreach (ActivityStatisticsModel activity in activityList)
                {
                    DataRow dr = dt.NewRow();
                    dr["活动名称"] = activity.ActivityName;
                    dr["总浏览量"] = activity.BrowseCount;
                    dr["浏览量(安卓)"] = activity.AnderBroCount;
                    dr["浏览量(苹果)"] = activity.IosBroCount;
                    dr["分享总次数"] = activity.ShareCount;
                    dr["分享次数(安卓)"] = activity.AnderShaCount;
                    dr["分享次数(苹果)"] = activity.IosShaCount;
                    dr["参与人数"] = activity.TotalCount;
                    dt.Rows.Add(dr);
                }

                //生成并下载Excel
                OfficeOperation.Export(dt, "活动点击统计", Response);
                
            }


你可能感兴趣的:(c#,Asp.Net,Aspose.Cells,Excel)