首页 > 编程语言 >C#List导出Excel

C#List导出Excel

时间:2023-09-07 14:07:00浏览次数:30  
标签:row1 CellStyle C# Excel List rowTemp CreateCell GetCell SetCellValue

1.导入NPOI

2.引用如下内容

using NPOI.XSSF.UserModel;

using NPOI.SS.UserModel;

using NPOI.HSSF.UserModel;

using System.IO;

3.List数据

1).如果是Json类型数据从前台传递到后台,用如下方式获取前台数据;

注:数据类是自定义的类;Key是前台传到后台的键名

List<数据类> list =

       Newtonsoft.Json.JsonConvert.DeserializeObject<List<数据类>>(context.Request["Key"]);

2).不是Json类型,C#后台直接写方法如下

List<数据类> list = GetList();

4.导出并保存Excel

       //创建HSSFWorkbook

       HSSFWorkbook excelBook = new HSSFWorkbook();

      //声明style1对象,设置Excel表格的样式

       ICellStyle style1 = excelBook.CreateCellStyle();

       ICellStyle style2 = excelBook.CreateCellStyle();

       ICellStyle style3 = excelBook.CreateCellStyle();

      //设置字体颜色及将字体颜色写入相应的样式里

       IFont font = excelBook.CreateFont();

       font.Color = IndexedColors.Red.Index;

       style3.SetFont(font);

      //设置样式的对齐方式

     //两端自动对齐(自动换行)

       style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Justify;

       style1.VerticalAlignment = VerticalAlignment.Center;

       style2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;

       style2.VerticalAlignment = VerticalAlignment.Center;

       style3.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Justify;

       style3.VerticalAlignment = VerticalAlignment.Center;

       //创建Excel工作表Sheet

       ISheet sheet1 = excelBook.CreateSheet("XXX信息");

       //给Sheet添加第一行的头部标题

       IRow row1 = sheet1.CreateRow(0);

       row1.CreateCell(0).SetCellValue("0");

       row1.CreateCell(1).SetCellValue("1");

       row1.CreateCell(2).SetCellValue("2");

       row1.CreateCell(3).SetCellValue("3");

       row1.CreateCell(4).SetCellValue("4");

       row1.CreateCell(5).SetCellValue("5");

       row1.CreateCell(6).SetCellValue("6");

      //注:row1.CreateCell(0).SetCellValue("0") SetCellValue里面的数字就是自定义的标题名

//标题初始化设置样式(可选)

       row1.GetCell(0).CellStyle = style2;

       row1.GetCell(1).CellStyle = style2;

       row1.GetCell(2).CellStyle = style2;

       row1.GetCell(3).CellStyle = style2;

       row1.GetCell(4).CellStyle = style2;

       row1.GetCell(5).CellStyle = style2;

       row1.GetCell(6).CellStyle = style2;

   //初始化设置宽度(可选)

       sheet1.SetColumnWidth(0, 10 * 256);

       sheet1.SetColumnWidth(0, 10 * 256);

       sheet1.SetColumnWidth(1, 10 * 256);

       sheet1.SetColumnWidth(2, 17 * 256);

       sheet1.SetColumnWidth(3, 30 * 256);

       sheet1.SetColumnWidth(4, 34 * 256);

       sheet1.SetColumnWidth(5, 20 * 256);

       sheet1.SetColumnWidth(6, 26 * 256);

       //添加数据行:给每一个单元格赋值

       for (int i = 0; i < list.Count; i++)

       {

           //创建行

           IRow rowTemp = sheet1.CreateRow(i + 1);

           rowTemp.Height = 62 * 20;

         //SetCellValue(list[i].xxx)里面的XXX是List里面的字段

           rowTemp.CreateCell(0).SetCellValue(list[i].xxx);

           rowTemp.CreateCell(1).SetCellValue(list[i].xxx);

           rowTemp.CreateCell(2).SetCellValue(list[i].xxx);

           rowTemp.CreateCell(3).SetCellValue(list[i].xxx);

           rowTemp.CreateCell(4).SetCellValue(list[i].xxx);

           rowTemp.CreateCell(5).SetCellValue(list[i].xxx);

           rowTemp.CreateCell(6).SetCellValue(Convert.ToDouble(list[i].xxx));

            //添加单元格样式(可选)

           rowTemp.GetCell(0).CellStyle = style3;

           rowTemp.GetCell(1).CellStyle = style3;

           rowTemp.GetCell(2).CellStyle = style2;

           rowTemp.GetCell(3).CellStyle = style2;

           rowTemp.GetCell(4).CellStyle = style1;

           rowTemp.GetCell(5).CellStyle = style1;

           rowTemp.GetCell(6).CellStyle = style1;

       }

  //保存的文件名

       string fileName = "信息" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

//文件保存路径(可自定义)

       FileStream bookStream = new FileStream(@"E:\xxx\xxx\" + fileName, FileMode.OpenOrCreate);

//将工作薄写入文件流

      excelBook.Write(bookStream);

      bookStream.Close();

标签:row1,CellStyle,C#,Excel,List,rowTemp,CreateCell,GetCell,SetCellValue
From: https://blog.51cto.com/u_14463418/7396358

相关文章

  • 奇妙的paramsPrepareParamsStack——Struts2中ModelDriven问题
    每次调用全文检索都要传入所有的参数值,不然导致当某个参数没有传值的时候但取到的却是上一次搜索提供的参数值。最后才发现原来是Action类里implementsModelDriven实现的原因:publicclassDocuIndexActionextendsBaseActionimplementsModelDriven{DocSearchParamdocSearchPa......
  • 下拉列表select动态初始化 (JSP页面)
    HTML代码:<td><selectid="as_occt"name="as_occt"><optionselected="selected"value="">智能模糊搜索</option><optionvalue="content">仅搜索内容</option><o......
  • sqlserver移植为Oracle笔记(更新,新增字段名;批量新增记录;日期查询;截取字串函数)
    下面是这两天在项目要sqlserver和oracle兼容的改造中测试出来的笔记:--sqlserver--更改主键字段名'ID'为'ID_'sp_rename  'tb_doc_cat_statistic.ID','ID_','column'--新增字段cat_codealtertabletb_doc_cat_statisticaddcat_codevarchar(100) --oracle--......
  • 【Redis】Could not create server TCP listening socket __6379_ bind_ 在一个非套接
    问题描述:安装Redis,redis-server无法启动,报错。[34420]20Aug21:43:21.110#oO0OoO0OoO0OoRedisisstartingoO0OoO0OoO0Oo[34420]20Aug21:43:21.110#Redisversion=5.0.14.1,bits=64,commit=ec77f72d,modified=0,pid=34420,juststarted[34420]20Aug21:43:21.1......
  • 深度合作SONiC社区,“一站式”SONiC网络解决方案赋能千行百业
    近日,星融元(asterfusion.com)正式加入成为Linux基金会下的SONiC社区会员单位成员,未来星融元将继续与SONiC社区深度合作,为人工智能、云、企业数据中心和园区接入打造一个开放、可扩展和可编程的网络架构。星融元是中国最早参与SONiC社区并积极向社区贡献缺陷修复和软件特性代码的公......
  • IndexSearch中增量索引使用reopen
    publicIndexSearchernewIndexSearcher(){ try{ if(null==isearcher){ isearcher=newIndexSearcher(IndexReader.open("D:/Index")); }else{ IndexReaderindexReader=isearcher.getIndexReader();//获取当前的indexReader if(!in......
  • Eclipse里做JBPM工作流gpd.xml中文乱码问题解决
         刚开始接到是做一个简单的文档借阅流程,对于流程定义是采用eclipse中的jbpm插件,但存在一个问题是节点中文命名的在gpd.xml中全部为乱码或根本看不到任何东西。     但是网上有人说没关系,这只是eclipse本身存在的一个bug,在项目所在硬盘目录下打开该文件还是显示正常......
  • npm:安装@vue/cli(5.0.8)
    一,安装@vue/cli:查看bin目录下的程序root@lhdpc:/usr/local/source/node#ls/usr/local/soft/node-v18.17.1-linux-x64/bin/corepack  node      npm       npx用npm安装root@lhdpc:/usr/local/source/node#npminstall-g@vue/cli查看bin目录下的程......
  • Epicor ERP成本稽核
    很多制造企业存在成本差异过大,公司要求提高成本准确率,以便为产品成本分析提供数据支撑。A.成本现状:成本差异分析,工时、费率、制造差异等出现各种不同情况,造成差异过大。B.以下是Epicor的成本模式,减少差异才是提高成本准确性的解决之道!C.如何避免差异的产生?  推荐企业设......
  • 【230908-1】(指数对数比大小)已知:a=log0.1_0.2,b=log1.1_0.2,c=1.1^0.2,则a,b,c的大小
    ......