PO主话:这里参考的主要是克隆数据行的格式

最近在用NPOI寫Excel範本套表程式,需要處理範本中需要迴圈處理的範本區塊,因此要將標示迴圈區塊的資料複製出來套表,因此找到一段不錯的Method來用,過幾天再分享比較完整的套表程式。

/// HSSFRow Copy Command /// Description: Inserts a existing row into a new row, will automatically push down /// any existing rows. Copy is done cell by cell and supports, and the /// command tries to copy all properties available (style, merged cells, values, etc...) private void CopyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum, int destinationRowNum) // Get the source / new row HSSFRow newRow = worksheet.GetRow(destinationRowNum); HSSFRow sourceRow = worksheet.GetRow(sourceRowNum); // If the row exist in destination, push down all rows by 1 else create a new row if (newRow != null) worksheet.ShiftRows(destinationRowNum, worksheet.LastRowNum, 1); newRow = worksheet.CreateRow(destinationRowNum); // Loop through source columns to add to new row for (int i = 0; i < sourceRow.LastCellNum; i++) // Grab a copy of the old/new cell HSSFCell oldCell = sourceRow.GetCell(i); HSSFCell newCell = newRow.CreateCell(i); // If the old cell is null jump to next cell if (oldCell == null) newCell = null; continue; // Copy style from old cell and apply to new cell HSSFCellStyle newCellStyle = workbook.CreateCellStyle(); newCellStyle.CloneStyleFrom(oldCell.CellStyle); ; newCell.CellStyle = newCellStyle; // If there is a cell comment, copy if (newCell.CellComment != null) newCell.CellComment = oldCell.CellComment; // If there is a cell hyperlink, copy if (oldCell.Hyperlink != null) newCell.Hyperlink = oldCell.Hyperlink; // Set the cell data type newCell.SetCellType(oldCell.CellType); // Set the cell data value switch (oldCell.CellType) case HSSFCellType.BLANK: newCell.SetCellValue(oldCell.StringCellValue); break; case HSSFCellType.BOOLEAN: newCell.SetCellValue(oldCell.BooleanCellValue); break; case HSSFCellType.ERROR: newCell.SetCellErrorValue(oldCell.ErrorCellValue); break; case HSSFCellType.FORMULA: newCell.SetCellFormula(oldCell.CellFormula); break; case HSSFCellType.NUMERIC: newCell.SetCellValue(oldCell.NumericCellValue); break; case HSSFCellType.STRING: newCell.SetCellValue(oldCell.RichStringCellValue); break; case HSSFCellType.Unknown: newCell.SetCellValue(oldCell.StringCellValue); break; // If there are are any merged regions in the source row, copy to new row for (int i = 0; i < worksheet.NumMergedRegions; i++) CellRangeAddress cellRangeAddress = worksheet.GetMergedRegion(i); if (cellRangeAddress.FirstRow == sourceRow.RowNum) CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.RowNum, (newRow.RowNum + (cellRangeAddress.FirstRow - cellRangeAddress.LastRow)), cellRangeAddress.FirstColumn, cellRangeAddress.LastColumn); worksheet.AddMergedRegion(newCellRangeAddress);

引用來源: http://www.zachhunter.com/2010/05/npoi-copy-row-helper/

转自 http://www.dotblogs.com.tw/kim66766/archive/2011/04/09/npoi-copy-row.aspx
PO主话:这里参考的主要是克隆数据行的格式最近在用NPOI寫Excel範本套表程式,需要處理範本中需要迴圈處理的範本區塊,因此要將標示迴圈區塊的資料複製出來套表,因此找到一段不錯的Method來用,過幾天再分享比較完整的套表程式。/// /// HSSFRow Copy Command////// Description: Inserts a existing row in
The interface is prepared for N POI 2.0 actually. In N POI 2.0, there are two namespace: XSSF and HSSF. XSSF is for xlsx, HSSF is for xls. Both implements interfaces in N POI .SS; N POI 有3种dll:①XSSF操作xlsx格式版本的 Excel ;②HSSF操作xls格式版本的 Excel ;③SS以上两种格式的 Excel 都可操作; 下面的方式
/// <param name="wb"></param> /// <param name="fromSheet"></param> /// <param name="toSheet"></param> /// <param name=" copy ValueFlag">&l... string TempletFileName = @"D:\template.xls"; File. Copy (TempletFileName, targetPath, true); HSSFWorkbook wk = null; using (FileStream fs = File.Open(targetPath, FileMode.Open...
HSSFWorkbook hssfworkbook = new HSSFWorkbook(); ISheet sheet = hssfworkbook.CreateSheet("Sheet1"); hssfworkbook.CreateSheet("Sheet2"); hssfworkbook.CreateSheet("Sheet3"); //Title I Row row = sh
这边我直接采用上一步生成的demo.xls. 直接上代码吧 也没有什么好说的: HSSFWorkbook hssfWorkbook; //加载 Excel 文件 using (var readFile = File.OpenRead(@"D:\demo.xls")) //文件对象 hssfWorkbook = new HSSFWorkbook(readFile)...
N POI 是一种用于操作Microsoft Office格式文件的.NET库。要创建一个合并行,可以使用N POI 的Sheet类中的Create Row 方法来创建一个新的行,并使用它的CreateCell方法在该行上创建单元格。然后,可以使用该行的Cells属性中的Merge方法来合并单元格。 下面是一个示例代码,演示如何使用N POI 创建一个合并行: ```csharp using N POI .SS.UserModel; // 获取要操作的工作表 ISheet sheet = workbook.GetSheet("Sheet1"); // 创建新行 I Row row = sheet.Create Row (0); // 在新行上创建单元格 ICell cell1 = row .CreateCell(0); ICell cell2 = row .CreateCell(1); ICell cell3 = row .CreateCell(2); // 设置单元格的值 cell1.SetCellValue("A"); cell2.SetCellValue("B"); cell3.SetCellValue("C"); // 合并单元格 sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 2)); 在上面的示例中,我们首先获取名为“Sheet1”的工作表,然后使用Create Row 方法创建一个新行。接下来,我们在该行上使用CreateCell方法创建三个单元格,并将它们的值分别设置为“A”、“B”和“C”。最后,我们使用AddMergedRegion方法来合并第 一行 的第一个、第二个和第三个单元格,以创建一个合并行。