如何做网站轮播图和菜单全屏,地方门户网站建设多少钱,wordpress的交叉表单,网站佣金怎么做会计分录NPOI是一个开源的C#读写Excel、WORD等微软OLE2组件文档的项目。
NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作。
相较于之前使用的Microsoft.Office.Interop.Excel#xff0c;已经感觉到的优势#xff0c;一是读写速度较快#xff0c;虽然小数据量的读…NPOI是一个开源的C#读写Excel、WORD等微软OLE2组件文档的项目。
NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作。
相较于之前使用的Microsoft.Office.Interop.Excel已经感觉到的优势一是读写速度较快虽然小数据量的读写还不是很明显但数据量上去后还是有差别的。二是Microsoft.Office.Interop.Excel会使用Office软件进行打开Excel的操作这不仅要求在电脑中安装对应的Office软件而且如果在程序中断的情况下后台会保留进程对后续的读写产生影响已经不只一次遇到这个问题。
因此果断放弃Microsoft.Office.Interop.Excel准备用NPOI代替。
下面是汇总的NPOI简单操作。 1、获取工作薄IWorkbook
FileStream fs new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
IWorkbook workbook new XSSFWorkbook(fs);
2、获取工作表对象ISheet
// 获取sheet,按序号从0开始
ISheet sheet workbook.GetSheetAt(0);
// 按名称获取
ISheet sheet workbook.GetSheet(sheet1);
3、获取工作表的行第一行
IRow row sheet.GetRow(0);
4、获取指定行的单元格
ICell cell row.GetCell(0);
5、获取并设置单元格样式
// 获取单元格格式
ICellStyle style cell.CellStyle;
// 设置格式
style.VerticalAlignmentHSSFCellStyle.VERTICAL_TOP // 靠上
style.VerticalAlignmentHSSFCellStyle.VERTICAL_CENTER // 居中
style.VerticalAlignmentHSSFCellStyle.VERTICAL_BOTTOM // 靠下
style.VerticalAlignmentHSSFCellStyle.VERTICAL_JUSTIFY // 两端对齐
// 自动换行
style.WrapTexttrue;
// 边框设置
style.BorderBottomHSSFCellStyle.BORDER_THIN;
style.BorderLeftHSSFCellStyle.BORDER_THIN;
style.BorderRightHSSFCellStyle.BORDER_THIN;
style.BorderTop HSSFCellStyle.BORDER_THIN ;
6、创建工作簿对象
XSSFWorkbook workBook new XSSFWorkbook();
7、创建工作表对象
XSSFSheet newSheet (XSSFSheet)workBook.CreateSheet(newSheet);
8、创建工作表的行
XSSFRow newRow (XSSFRow)newSheet.CreateRow(0);
9、创建单元格
XSSFCell newCell (XSSFCell)newRow.CreateCell(0);
10、单元格写值
newCell.SetCellValue(1);
11、设置Sheet名称
workBook.SetSheetName(0, newSheet);
12、获取工作簿中Sheet数量
int count workBook.NumberOfSheets;
13、保存excel文件
workBook.Write(new FileStream(outFilePath, FileMode.Create, FileAccess.ReadWrite));
14、强制更新计算公式
sheet.ForceFormulaRecalculation true;
15、完整的写值操作
// 文件路径
string filePath C:\Users\Administrator\Desktop\lab.xlsx;
// 创建文件流
FileStream fs File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// 打开工作簿
XSSFWorkbook wb new XSSFWorkbook(fs);
// 获取第一个工作表
ISheet sheet wb.GetSheet(new);
for (int i 0; i 100; i)
{// 获取第3行索引从0开始IRow row sheet.GetRow(i);for (int j 0; j 30; j){// 获取第5列索引从0开始ICell cell row.GetCell(j);// 设置单元格的值为新cell?.SetCellValue(fme);}
}
// 保存工作簿
using FileStream saveFile new FileStream(filePath, FileMode.Create);
wb.Write(saveFile);
16、单元格合并
// 下面2345指代的意思为从第3行第4列开始至第5行第6列进行合并
// 即为“C4:E6”
sheet.AddMergedRegion(new Region(2, 3, 4, 5));
17、设置、获取行高列宽
// 获取当前工程中的所有Layouts
IEnumerableLayoutProjectItem layouts Project.Current.GetItemsLayoutProjectItem();
// 按名称获取
LayoutProjectItem layoutItem Project.Current.GetItemsLayoutProjectItem().FirstOrDefault(item item.Name.Equals(MyLayout));
18、获取sheet的行数列数
// 行数
int rowNum sheet.LastRowNum;
// 列数
int colNum sheet.LastRowNum;
19、移动行
// 将第9行到第40行的内容向上移动2格
sheet.ShiftRows(8, 39, -2);
20、取消sheet中的所有合并格
// 获取所有合并区域
ListCellRangeAddress mergeRanges sheet.MergedRegions;
// 检查并清除合并区域
for (int i mergeRanges.Count - 1; i 0; i--)
{// 合并格的四至CellRangeAddress region mergeRanges[i];int firstRow region.FirstRow;int lastRow region.LastRow;int firstCol region.FirstColumn;int lastCol region.LastColumn;// 判定要处理的区域if (startRow firstRow startCol firstCol){for (int row firstRow; row lastRow; row){for (int col firstCol; col lastCol; col){if (row ! firstRow || col ! firstCol){IRow r sheet.GetRow(row);ICell c r.GetCell(col);// 如果c是空值则赋一个默认值c ?? r.CreateCell(col);// 设置拥有合并区域的单元格的值为合并区域的值ICell mergedCell sheet.GetRow(firstRow).GetCell(firstCol);if (mergedCell ! null){c.SetCellValue(mergedCell.StringCellValue); // 可根据需要选择相应的数据类型}}}}// 清除合并区域sheet.RemoveMergedRegion(i);}
} 文章转载自: http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.rrwgh.cn.gov.cn.rrwgh.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.lzqdl.cn.gov.cn.lzqdl.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn http://www.morning.lwdzt.cn.gov.cn.lwdzt.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.kwqt.cn.gov.cn.kwqt.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn http://www.morning.jmspy.cn.gov.cn.jmspy.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.ffydh.cn.gov.cn.ffydh.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn