当前位置: 首页 > 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.15wanjia.com/news/6571.html

相关文章:

  • 武汉网站建设多少钱信阳网络推广公司
  • 做网站公司运营部网站的seo方案
  • 苏州做网站最好公司百度seo培训班
  • wordpress css修改字体最好的seo外包
  • 适合个人网站谷歌代理
  • 做网站类型的营业执照证明如何填写百度一下网页搜索
  • 天权网站建设网络营销的工具有哪些
  • 网站开发税收分类金花站长工具
  • 小说网站开发需求分析推广哪些app最挣钱
  • wordpress 4.5.3中太原建站seo
  • 做网站需要多少钱知乎超级优化空间
  • 商城推广 网站建设宁波seo推广
  • 大连软件培训淄博网站制作优化
  • 如何在网站上做免费代理yahoo搜索
  • wordpress 内容编辑器北海百度seo
  • wordpress4.8发布高级seo招聘
  • 设计个网站需要怎么做北京seo排名外包
  • 管理网站怎么做seo站长综合查询工具
  • django做网站想要网站导航正式推广
  • 蓟县做网站公司友情链接出售平台
  • 网站建设平台还有没有趋势seo整站优化费用
  • 没有网站百度推广高级seo培训
  • 佛山大沥上海百度seo优化
  • dw做网站视频教程职业培训网络平台
  • 如何建立一个个人网站优化方案丛书官网
  • 怎么把网站做seo到首页企业培训课程ppt
  • 网站关键词整体方案泰安短视频seo
  • 三维在线设计网站找合作项目app平台
  • markdown做网站上海网络推广软件
  • 锦州北京网站建设seo网络推广哪家专业