npoi导出excel中左上角有绿色三角的解决方案和npoi的相关设置

npoi导出excel中左上角有绿色三角的解决方案:

这是因为 这是对数据的判断,插入11等数字类型时插入的是“11”。

所以:

foreach (DataColumn column in sourceDs.Tables[i].Columns) { dataRow.CreateCell(0).SetCellValue(indexnum); // 第column.Ordinal列 string str = row[column].ToString(); if (double.TryParse(str, out outdouble) == true)//判断是否是数字类型 { dataRow.CreateCell(column.Ordinal + 1).SetCellValue(outdouble); } else { dataRow.CreateCell(column.Ordinal + 1).SetCellValue(str); } // 数据行样式 dataRow.Cells[column.Ordinal + 1].CellStyle = rowData; dataRow.Cells[0].CellStyle = rowData; }

关于npoi设置固定宽度

sheet.SetColumnWidth(addCellNum, 13 * 256); //设置自定义宽度

第一个参数表示从哪一个列开始设置宽度为13个字符,第二个参数表示设置多少字符,汉子占两个字符


关于npoi合并单元格

sheet.AddMergedRegion(new CellRangeAddress(startCell, startCell, cellnum, addCellNum - 1));

参数: 开始行,结束行,开始列,结束列   合并开始列至结束列之间的单元格


关于设置单元格格式

           

            //设置数据行样式ICellStyle rowData = workbook.CreateCellStyle();IFont fontData = workbook.CreateFont();rowData.Alignment = HorizontalAlignment.Center;水平居中rowData.VerticalAlignment = VerticalAlignment.Center;垂直居中rowData.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;加边框rowData.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;rowData.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;rowData.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;rowData.WrapText = true;是否自动换行rowData.Indention = 30;文本缩进rowData.BottomBorderColor = HSSFColor.Red.Index;底部边框颜色rowData.FillForegroundColor = HSSFColor.Blue.Index;//图案颜色rowData.FillBackgroundColor = HSSFColor.Yellow.Index;//背景颜色fontData.FontHeightInPoints =11;字体大小fontData.FontName = "宋体";字体样式fontData.Color = HSSFColor.Red.Index;字体颜色rowData.SetFont(fontData);
设置单元格的宽度和高度
设置第一行的高度  sheet10.CreateRow(0).Height = 200*20; 
设置通用宽度 sheet10.DefaultColumnWidth=100*256;  
设置通用高度 sheet10.DefaultRowHeight=30*20;   
获取某列宽度 int col1width = sheet10.GetColumnWidth(1);  

关于npoi自适应宽度

if(currentRow.GetCell(columnNum) != null){ICell currentCell = currentRow.GetCell(columnNum);int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;if (columnWidth < length){columnWidth = length;}sheet.SetColumnWidth(columnNum, columnWidth * 256);}


 

关于npoi自适应高度
for (int rowNum = 2; rowNum <= ffSheet.LastRowNum; rowNum++){IRow currentRow = ffSheet.GetRow(rowNum);ICell currentCell = currentRow.GetCell(27);int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;currentRow.HeightInPoints = 20 * (length / 60 + 1);}


 
 
 





本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部