List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet shiftRows
@Override public void shiftRows(int startRow, int endRow, final int n, boolean copyRowHeight, boolean resetOriginalRowHeight)
From source file:com.heimaide.server.common.utils.excel.ExportExcel.java
License:Open Source License
private void exportFile(List<String> list, String rootPath, HttpServletResponse response, List<List<Object>> bodyList) throws FileNotFoundException, IOException { String importFileName = rootPath + "WEB-INF" + File.separator + "xlsx" + File.separator + "order.xlsx"; String tmpDir = rootPath + "tmpDir" + File.separator; XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(importFileName)); this.styles = createStyles(wb); //?excel? //from www . ja v a2 s . c om //??sheet XSSFSheet sheet = wb.getSheetAt(0); XSSFCell cell1 = sheet.getRow(1).getCell(0); String cell1str = cell1.getStringCellValue(); //???? cell1str = cell1str.replace("X", list.get(0)); cell1.setCellValue(cell1str); XSSFCell cell2 = sheet.getRow(2).getCell(0); String cell2str = cell2.getStringCellValue(); //?? cell2str = cell2str.replace("X", list.get(1)); //? cell2str = cell2str.replace("Y", list.get(2)); //??? cell2str = cell2str.replace("Z", list.get(3)); cell2.setCellValue(cell2str); XSSFCell cell3 = sheet.getRow(3).getCell(12); String cell3str = cell3.getStringCellValue(); cell3str = cell3str.replace("XX", list.get(4)); cell3.setCellValue(cell3str); XSSFCell cell4 = sheet.getRow(sheet.getLastRowNum() - 1).getCell(0); String cell4str = cell4.getStringCellValue(); cell4str = cell4str.replace("X", list.get(5)); cell4.setCellValue(cell4str); XSSFCell cell5 = sheet.getRow(sheet.getLastRowNum() - 4).getCell(12); XSSFCell cell6 = sheet.getRow(sheet.getLastRowNum() - 4).getCell(13); String str1 = String.valueOf(list.get(6)); String str2 = String.valueOf(list.get(7)); str1 = str1.substring(0, str1.indexOf(".")); str2 = str2.substring(0, str2.indexOf(".")); cell5.setCellValue(str1); cell6.setCellValue(str2); XSSFCell cell7 = sheet.getRow(sheet.getLastRowNum() - 3).getCell(12); XSSFCell cell8 = sheet.getRow(sheet.getLastRowNum() - 3).getCell(13); cell7.setCellValue(Double.parseDouble(list.get(6)) / 10); cell8.setCellValue(Double.parseDouble(list.get(7)) / 10); // int columnNum =sheet.getRow(3).getLastCellNum(); // int column =0; // for(int i=0;i<columnNum;i++) // { // System.out.println("==="+sheet.getRow(3).getCell(i).getStringCellValue()); // column =i; // } int num = 5; for (int i = 0; i < bodyList.size(); i++) { List<Object> strList = bodyList.get(i); sheet.shiftRows(num, bodyList.size() + num, 1, true, false); sheet.createRow(num); for (int j = 0; j < strList.size(); j++) { Row row = sheet.getRow(num); addCell(row, j, strList.get(j)); // Cell cell = row.createCell(j); // cell.setCellValue(strList.get(j)); } num++; } //?? String tmpName = tmpDir + "tmp_hmd.xlsx"; File dirFile = new File(tmpDir); if (!dirFile.exists()) { dirFile.mkdir(); } File tmpFile = new File(tmpName); FileOutputStream fos = new FileOutputStream(tmpFile); wb.write(fos); XSSFWorkbook wb1 = new XSSFWorkbook(new FileInputStream(tmpName)); response.reset(); response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + Encodes.urlEncode("?" + list.get(0) + "?.xlsx")); wb1.write(response.getOutputStream()); tmpFile.delete(); }
From source file:de.bund.bfr.knime.openkrise.db.imports.custom.bfrnewformat.TraceGenerator.java
License:Open Source License
private XSSFRow copyRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum, int destinationRowNum) { XSSFRow sourceRow = worksheet.getRow(sourceRowNum); worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1, true, false); XSSFRow newRow = worksheet.createRow(destinationRowNum); // Loop through source columns to add to new row for (int i = 0; i < sourceRow.getLastCellNum(); i++) { // Grab a copy of the old/new cell XSSFCell oldCell = sourceRow.getCell(i); XSSFCell newCell = newRow.createCell(i); // If the old cell is null jump to next cell if (oldCell == null) { newCell = null;/*from w w w. ja v a 2s.com*/ continue; } // Copy style from old cell and apply to new cell XSSFCellStyle newCellStyle = workbook.createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); newCell.setCellStyle(newCellStyle); // Set the cell data type newCell.setCellType(oldCell.getCellType()); } // If there are are any merged regions in the source row, copy to new row for (int i = 0; i < worksheet.getNumMergedRegions(); i++) { CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) { CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(), (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn()); worksheet.addMergedRegion(newCellRangeAddress); } } newRow.setHeight(sourceRow.getHeight()); return newRow; }