当前位置: 首页 > news >正文

怎么看是哪家做的网站抚宁网站建设

怎么看是哪家做的网站,抚宁网站建设,简速做网站工作室,学校网站建设开发商NuGet中安装npoi 创建excel模板,替换其中的内容生成新的excel文件。 例子中主要写了这四种情况: 1、替换单个单元格内容; 2、替换横向多个单元格; 3、替换表格; 4、单元格中插入图片; using System.IO; …

NuGet中安装npoi

创建excel模板,替换其中的内容生成新的excel文件。

例子中主要写了这四种情况:

1、替换单个单元格内容;
2、替换横向多个单元格;
3、替换表格;
4、单元格中插入图片;

using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.Diagnostics;public static void exportExcel(){string currentDirectory = System.AppDomain.CurrentDomain.BaseDirectory;string templatePath = currentDirectory + "/template.xlsx";string outputPath = currentDirectory + "/output/" + DateTime.Now.ToString("yyyyMMdd"); //存储路径string outputPathExcel = outputPath + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_test.xlsx";// 检查路径是否存在if (!Directory.Exists(outputPath)){Directory.CreateDirectory(outputPath);}//1、单元格直接替换部分Dictionary<string, string> replacements = new Dictionary<string, string>{{ "test1", "测试1" },{ "test2", "测试2" }};//2、横向数组,曲线表格Dictionary<string, double[]> replacementsArrayH = = new Dictionary<string, double[]>();replacementsArrayH["item1"] = new double[] { 1.1, 2.2, 3.3 };replacementsArrayH["item2"] = new double[] { 4.4, 5.5, 6.6 };//3、表格数据//自己定义类//public TaskItem{//  private string name;//  private string age;//  public TaskItem(){//  }//  public TaskItem(string name, string age){//	  this.name=name;//	  this.age=age;//  }//  ……//}List<TaskItem> taskItemList = new List<TaskItem>();taskItemList.Add(new TaskItem("Alice", "30"));taskItemList.Add(new TaskItem("Bob", "25"));try{// excel导出using (FileStream file = new FileStream(templatePath, FileMode.Open, FileAccess.Read)){IWorkbook workbook = new XSSFWorkbook(file);ISheet sheet = workbook.GetSheetAt(0);// 替换模板中的字符串for (int rowIndex = 0; rowIndex <= sheet.LastRowNum; rowIndex++){IRow row = sheet.GetRow(rowIndex);if (row == null){continue;}for (int cellIndex = 0; cellIndex < row.LastCellNum; cellIndex++){ICell cell = row.GetCell(cellIndex);if (cell != null && cell.CellType == CellType.String){string cellValue = cell.StringCellValue;//填充单个文本foreach (var replacement in replacements){if (cellValue.Equals(replacement.Key)){cell.SetCellValue(cellValue.Replace(replacement.Key, replacement.Value));}}//填充横向数组foreach (var replacement in replacementsArrayH){string placeholder = replacement.Key;double[] values = replacement.Value;int index = values.Length;if (cellValue.Equals(replacement.Key)){for (int colNum = 0; colNum < index; colNum++){cell = row.GetCell(cellIndex + colNum);cell.SetCellValue(cellValue.Replace(placeholder, values[colNum].ToString()));}}}//填充详情表格if (cellValue.Equals("table")){if (list == null) // list 为业务需要写入数据{continue;}// 添加数据行for (var i = 0; i < list.Count; i++){TaskItem item = list[i];// 因为模版内已经存在一格空行,所以直接赋值,后续从此行进行 CopyRow 即可if (i == 0){SetRowCellValue(row, item, cellIndex);continue;}//其他行var newRow = sheet.CopyRow(rowIndex, rowIndex + i);SetRowCellValue(newRow, item, cellIndex);}}}}}//4、导出图片string picPath = currentDirectory + "/test.png";//图片byte[] bytes = File.ReadAllBytes(picPath);int pictureIndex = workbook.AddPicture(bytes, PictureType.PNG);// 定义图片的位置var drawingPatriarch = sheet.CreateDrawingPatriarch();var anchor = drawingPatriarch.CreateAnchor(0, 0, 0, 0, 1, 6, 12, 13); // 参数依次是:dx1, dy1, dx2, dy2, col1, row1, col2, row2// 在单元格中添加图片var picture = drawingPatriarch.CreatePicture(anchor, pictureIndex);// 保存新文件using (FileStream outputFile = new FileStream(outputPathExcel, FileMode.Create, FileAccess.Write)){workbook.Write(outputFile);}}}catch (Exception ex){}}/// <summary>/// 填充表格数据/// </summary>/// <param name="row">填充行</param>/// <param name="item">填充数据</param>/// <param name="cellIndex">列开始行</param>private static void SetRowCellValue(IRow row, TaskItem item, int cellIndex){ICell cell = row.GetCell(cellIndex++);cell.SetCellValue(item.name);cellIndex++;cell = row.GetCell(cellIndex++);cell.SetCellValue(item.age);}

excel模板内容:

导出后:

http://www.yayakq.cn/news/558328/

相关文章:

  • 初做淘宝客选哪个网站百度发布信息的免费平台
  • dede产品展示网站模板卖酒网站排名
  • 传奇新开网站优化什么
  • 江西网站开发多少钱seo方法图片
  • h5网站开发的文献综述重庆国外网站推广
  • 天津企业模板建站哪个好网站建设收费标准资讯
  • 高端制作网站技术四川省住房和城乡建设厅网站是多少
  • 网站栏目变了怎么做跳转网站整合营销推广
  • 做网站如何分页软件开发工程师绩效考核
  • 南城网站建设公司如何网站注销
  • 杭州最大的网站开发深圳北站设计者
  • 网站类型分类国内最大的网站建设公司排名
  • 很简单的网站wordpress侧边浮动
  • 做培训网站海阳seo排名优化培训
  • 陕西省建设网三类人员证书查询系统烟台做网站优化哪家好
  • 门户营销型网站创业商机网农村
  • 免费推广网站入口2022wordpress如何发送邮件
  • 侨联 文化宣传 侨联网站建设加盟推广公司
  • 白云区网站建设mg126北京 建公司网站要多少钱
  • 国办网站建设规范腾讯云网站搭建
  • 微信群推广佣金平台网站seo三要素
  • 益阳学校网站建设北京专业网站制作介绍
  • 巨野做网站的建设一个购物网站流程
  • 莆田专业网站建设公司青岛官网seo技术厂家
  • 口碑好的定制网站建设公司哪家好wordpress 注册图片
  • 做百度网站找谁企业网站的建设公司价格
  • 重庆大足网站建设网站建设丿金手指下拉
  • 怎么做学校网站和微信公众号效果图参考网站有哪些
  • 制作一个专门浏览图片的网站项目推广计划书
  • 外贸建站推广公司成华网站制作