C# Aspose 生成excel文件

≡放荡痞女 提交于 2020-03-15 07:07:00
        //using Aspose.Cells;  下载aspose.dll
        public static void createExcel()
        {
            Workbook wb = new Workbook();
            Worksheet sheet = wb.Worksheets[0];
            //设置样式
            Style style = wb.CreateStyle();
            style.ForegroundColor = System.Drawing.Color.FromArgb(199, 214, 157);
            style.HorizontalAlignment = TextAlignmentType.Center;
            style.Pattern = BackgroundType.Solid;
            //绑定数据
            sheet.Cells[0, 0].PutValue("工号1");
            sheet.Cells[0, 1].PutValue("姓名");
            //绑定样式
            sheet.Cells[0, 0].SetStyle(style);
            sheet.Cells[0, 1].SetStyle(style);
            wb.Save(@"F:/期间模板1.xlsx");
        }

 //MVC 控制器返回文件流,设置响应头

//方法返回文件流
        public static  MemoryStream  createExcel()
        {
            Workbook wb = new Workbook();
            Worksheet sheet = wb.Worksheets[0];
            //设置样式
            Style style = wb.CreateStyle();
            style.ForegroundColor = System.Drawing.Color.FromArgb(199, 214, 157);
            style.HorizontalAlignment = TextAlignmentType.Center;
            style.Pattern = BackgroundType.Solid;
            //绑定数据
            sheet.Cells[0, 0].PutValue("工号1");
            sheet.Cells[0, 1].PutValue("姓名");
            //绑定样式
            sheet.Cells[0, 0].SetStyle(style);
            sheet.Cells[0, 1].SetStyle(style);
            return wb.SaveToStream();
        }


//控制器设置响应头 接收文件流
            var excel = createExcel();
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "utf-8";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "排班模板.xls"));
            Response.Clear();
            excel.WriteTo(Response.OutputStream);
            Response.End();

 

            //excel设置列格式为文本样式
            Style st = wb.CreateStyle();
            st.Number = 49;
            StyleFlag flag = new StyleFlag();
            flag.NumberFormat = true;
            sheet.Cells.Columns[0].ApplyStyle(st, flag);

 

 

Aspose:DownLoad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!