zoukankan      html  css  js  c++  java
  • C# Aspose 生成excel文件

            //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

  • 相关阅读:
    LA 6439 Pasti Pas! Hash
    HDU 1067 Gap BFS+Hash
    POJ 3474 Gold Balanced Lineup Hash
    ZOJ 3802 Easy 2048 Again 状压DP
    Hihocoder #1044 状态压缩·一
    HDU 2522 & AOJ 441 & AOJ 364 关于小数和分数的转换
    HDU 2549 Sumset Hash+枚举
    POJ 1840 Eqs Hash + 中途相遇法
    HDU 2128 Tempter of the Bone II BFS
    POJ 3686 & 拆点&KM
  • 原文地址:https://www.cnblogs.com/tangpeng97/p/7850243.html
Copyright © 2011-2022 走看看