NPOI导入sheet,并复制多行数据
1、要将其他文件的sheet导入到一个excel文件里
2、使用CopyTo方法,单行复制;
void Main() { IWorkbook workbook1 =null; workbook1 = new XSSFWorkbook(@"C:\temp\访谈记录(202210)01.xlsx"); XSSFSheet sheet1 = (XSSFSheet) workbook1.GetSheetAt(0); IWorkbook workbook2 = null; workbook2 = new XSSFWorkbook(@"C:\temp\访谈记录(202210)02.xlsx"); XSSFSheet sheet2 = (XSSFSheet) workbook2.GetSheetAt(0); IWorkbook workbook3 = null; workbook3 = new XSSFWorkbook(@"C:\temp\访谈记录(202210)03.xlsx"); XSSFSheet sheet3 = (XSSFSheet) workbook3.GetSheetAt(0); //将sheet复制到一个文件里 sheet2.CopyTo(workbook1, "02", true, true); sheet3.CopyTo(workbook1, "03", true, true); //读取相应的sheet XSSFSheet sheet1_2 = (XSSFSheet) workbook1.GetSheetAt(2); XSSFSheet sheet1_3 = (XSSFSheet) workbook1.GetSheetAt(3); //找出sheet1的第一个空行index //复制行CopyRow(ISheet sourceSheet, int sourceRowIndex, ISheet targetSheet, int targetRowIndex) SheetUtil.CopyRow(sheet1_2, 3, sheet1, 9); SheetUtil.CopyRow(sheet1_2, 4, sheet1, 10); SheetUtil.CopyRow(sheet1_2, 5, sheet1, 11); SheetUtil.CopyRow(sheet1_2, 6, sheet1, 12); SheetUtil.CopyRow(sheet1_2, 7, sheet1, 13); SheetUtil.CopyRow(sheet1_2, 8, sheet1, 14); SheetUtil.CopyRow(sheet1_3, 3, sheet1, 15); SheetUtil.CopyRow(sheet1_3, 4, sheet1, 16); SheetUtil.CopyRow(sheet1_3, 5, sheet1, 17); FileStream file = new FileStream(@"C:\temp\qq"+ DateTime.Now.ToString("MMddHHmmss") +".xlsx",FileMode.Create); workbook1.Write(file,true); file.Close(); }