标签:相同 style ott byte ali column type npoi raw
以下代码使用的NPOI版本为2.3.0
记录一些NPOI操作的代码
1.读取模板并且获取第一个sheet
1 string template = ExcelTemplatePath + "PFMEATemplate.xlsx"; 2 FileStream fileStream = new FileStream(template, FileMode.Open, FileAccess.Read); 3 ISheet sheet = null; 4 XSSFWorkbook xssfworkbook = new XSSFWorkbook(fileStream); 5 sheet = xssfworkbook.GetSheetAt(0);
2.读取单元格
1 IRow Row = sheet.GetRow(i); //获取行,i=0为第一行 2 ICell cell = Row.GetCell(j); //获取单元格,此处为第i行,第j列的单元格 3 cell.ToString() //单元格内容 4 cell.SetCellValue("123"); //给单元格赋值123
3.几个常用的样式
#region 设置几个常用的样式 IFont font = xssfworkbook.CreateFont(); font.FontHeightInPoints = 9; font.FontName = "宋体"; ICellStyle style = xssfworkbook.CreateCellStyle(); style.BorderBottom = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; style.BorderTop = BorderStyle.Thin; style.VerticalAlignment = VerticalAlignment.Center; style.Alignment = HorizontalAlignment.Center; style.SetFont(font); style.WrapText = true; ICellStyle styleGreen = xssfworkbook.CreateCellStyle(); styleGreen.BorderBottom = BorderStyle.Thin; styleGreen.BorderLeft = BorderStyle.Thin; styleGreen.BorderRight = BorderStyle.Thin; styleGreen.BorderTop = BorderStyle.Thin; styleGreen.FillPattern = FillPattern.SolidForeground; styleGreen.FillForegroundColor = GetXLColour(Color.DarkGreen); styleGreen.VerticalAlignment = VerticalAlignment.Center; styleGreen.Alignment = HorizontalAlignment.Center; styleGreen.SetFont(font); ICellStyle styleRed = xssfworkbook.CreateCellStyle(); styleRed.BorderBottom = BorderStyle.Thin; styleRed.BorderLeft = BorderStyle.Thin; styleRed.BorderRight = BorderStyle.Thin; styleRed.BorderTop = BorderStyle.Thin; styleRed.FillPattern = FillPattern.SolidForeground; styleRed.FillForegroundColor = GetXLColour(Color.Red); styleRed.VerticalAlignment = VerticalAlignment.Center; styleRed.Alignment = HorizontalAlignment.Center; styleRed.SetFont(font); ICellStyle styleYellow = xssfworkbook.CreateCellStyle(); styleYellow.BorderBottom = BorderStyle.Thin; styleYellow.BorderLeft = BorderStyle.Thin; styleYellow.BorderRight = BorderStyle.Thin; styleYellow.BorderTop = BorderStyle.Thin; styleYellow.FillPattern = FillPattern.SolidForeground; styleYellow.FillForegroundColor = GetXLColour(Color.Yellow); styleYellow.VerticalAlignment = VerticalAlignment.Center; styleYellow.Alignment = HorizontalAlignment.Center; styleYellow.SetFont(font); ICellStyle styleMerage = xssfworkbook.CreateCellStyle(); styleMerage.BorderBottom = BorderStyle.Thin; styleMerage.BorderLeft = BorderStyle.Thin; styleMerage.BorderRight = BorderStyle.Thin; styleMerage.BorderTop = BorderStyle.Thin; styleMerage.VerticalAlignment = VerticalAlignment.Center; styleMerage.Alignment = HorizontalAlignment.Center; styleMerage.WrapText = true; styleMerage.SetFont(font); #endregion
ICell cell0 = dataRow.CreateCell(0); cell0.CellStyle = style; //给cell0单元格设定style样式
4.加载图片
//加载图片到workbook public int LoadImage(string pircture, XSSFWorkbook xssfworkbook) { using (FileStream file = new FileStream(pircture, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[file.Length]; file.Read(buffer, 0, (int)file.Length); file.Flush(); return xssfworkbook.AddPicture(buffer, PictureType.PNG); } } //将加载好的图片放到指定单元格 IDrawing drawing = sheet.CreateDrawingPatriarch(); XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, picColumn, i+9, picColumn+1, i + 9+1); //XSSFClientAnchor构造函数的参数依次是图片左上角的单元格x偏移量、y偏移量、图片右下角x偏移量、y偏移量、左上角单元格列、左上角单元格行、右下角单元格列、右下角单元格行 XSSFPicture xssfpicture = (XSSFPicture)drawing.CreatePicture(anchor, picColumn); //pieColumn为LoadImage函数返回的int索引
5.画线
XSSFDrawing linedrawing = (XSSFDrawing)sheet.CreateDrawingPatriarch(); XSSFClientAnchor lineAnchor = new XSSFClientAnchor(10, 10, 10, 10, 3, 15, 3, 18); XSSFSimpleShape lineShape = linedrawing.CreateSimpleShape(lineAnchor); lineShape.ShapeType = 1; lineShape.SetLineStyleColor(0,0,0);
第4点、第5点有bug,设置的XSSFClientAnchor中的前4个变量偏移量不起作用(如果有哪位大神知道理由请指导),后将
XSSFClientAnchor对象改为HSSFClientAnchor对象,
这两个对象的区别
XSSFClientAnchor对应xlsx后缀
HSSFClientAnchor对应xls后缀
实际使用没有多少差别,生成相同的文件xlsx的大小比xls的大小小一些,实际体验基本没有区别
附上使用HSSFClientAnchor的代码
IDrawing drawing = sheet.CreateDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(250, 0, 250, 0, picColumn, i + 9, picColumn + 1, i + 9 + 1); HSSFPicture hssfpicture = (HSSFPicture)drawing.CreatePicture(anchor, picColumn + 1); hssfpicture.Resize(); //画直线 HSSFPatriarch linedrawing = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); HSSFClientAnchor lineAnchor = new HSSFClientAnchor(500, 220, 500, 0, preCol, preRow, picColumn, i + 9); HSSFSimpleShape lineShape = linedrawing.CreateSimpleShape(lineAnchor); lineShape.ShapeType = HSSFSimpleShape.OBJECT_TYPE_LINE; lineShape.SetLineStyleColor(0, 0, 0);
标签:相同 style ott byte ali column type npoi raw
原文地址:https://www.cnblogs.com/lovejunjuan/p/12123963.html