यह एक कार्यपुस्तिका से दूसरी कार्यपत्रियों की प्रतिलिपि बनाने का मेरा कार्यान्वयन है। यह समाधान मेरे लिए काम करता है। यह कोड काम करेगा यदि चादरों में टेबल नहीं हैं, आदि। यदि चादरों में सरल टेक्स्ट (स्ट्रिंग, बूलियन, इंट इत्यादि), सूत्र होते हैं, तो यह समाधान काम करेगा।
Workbook oldWB = new XSSFWorkbook(new FileInputStream("C:\\input.xlsx"));
Workbook newWB = new XSSFWorkbook();
CellStyle newStyle = newWB.createCellStyle(); // Need this to copy over styles from old sheet to new sheet. Next step will be processed below
Row row;
Cell cell;
for (int i = 0; i < oldWB.getNumberOfSheets(); i++) {
XSSFSheet sheetFromOldWB = (XSSFSheet) oldWB.getSheetAt(i);
XSSFSheet sheetForNewWB = (XSSFSheet) newWB.createSheet(sheetFromOldWB.getSheetName());
for (int rowIndex = 0; rowIndex < sheetFromOldWB.getPhysicalNumberOfRows(); rowIndex++) {
row = sheetForNewWB.createRow(rowIndex); //create row in this new sheet
for (int colIndex = 0; colIndex < sheetFromOldWB.getRow(rowIndex).getPhysicalNumberOfCells(); colIndex++) {
cell = row.createCell(colIndex); //create cell in this row of this new sheet
Cell c = sheetFromOldWB.getRow(rowIndex).getCell(colIndex, Row.CREATE_NULL_AS_BLANK); //get cell from old/original WB's sheet and when cell is null, return it as blank cells. And Blank cell will be returned as Blank cells. That will not change.
if (c.getCellType() == Cell.CELL_TYPE_BLANK){
System.out.println("This is BLANK " + ((XSSFCell) c).getReference());
}
else { //Below is where all the copying is happening. First It copies the styles of each cell and then it copies the content.
CellStyle origStyle = c.getCellStyle();
newStyle.cloneStyleFrom(origStyle);
cell.setCellStyle(newStyle);
switch (c.getCellTypeEnum()) {
case STRING:
cell.setCellValue(c.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cell.setCellValue(c.getDateCellValue());
} else {
cell.setCellValue(c.getNumericCellValue());
}
break;
case BOOLEAN:
cell.setCellValue(c.getBooleanCellValue());
break;
case FORMULA:
cell.setCellValue(c.getCellFormula());
break;
case BLANK:
cell.setCellValue("who");
break;
default:
System.out.println();
}
}
}
}
}
//Write over to the new file
FileOutputStream fileOut = new FileOutputStream("C:\\output.xlsx");
newWB.write(fileOut);
oldWB.close();
newWB.close();
fileOut.close();
यदि आपकी आवश्यकता पूरी चीज को छोड़कर या कुछ भी जोड़ने के बिना कॉपी करना है। मुझे लगता है कि उन्मूलन की प्रक्रिया उपर्युक्त कोड के बाद बेहतर और तेज काम करती है। और अगर आप को खोने सूत्र, चित्र, टेबल, शैली, फोंट, आदि के बारे में
XSSFWorkbook wb = new XSSFWorkbook("C:\\abc.xlsx");
for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) {
if (!wb.getSheetName(i).contentEquals("January")) //This is a place holder. You will insert your logic here to get the sheets that you want.
wb.removeSheetAt(i); //Just remove the sheets that don't match your criteria in the if statement above
}
FileOutputStream out = new FileOutputStream(new File("C:\\xyz.xlsx"));
wb.write(out);
out.close();
स्रोत
2016-12-21 04:47:15
यह [कड़ी] (http://www.coderanch.com/t/420958/open-source में चिंता करने की जरूरत नहीं है/प्रतिलिपि-शीट-एक्सेल-फ़ाइल-अन्य) सहायक होना चाहिए: – hkansal
@ हंसलल जो लिंक मुझे मिला जब मैं गुगल रहा था। मुझे उस लिंक में दिए गए कोड के साथ एक समस्या का सामना करना पड़ा, जब सेल को स्तंभ 1 के रूप में समूहीकृत किया गया है: ए 4 यह विलय क्षेत्र को ओवरलैप करने जैसी त्रुटि दिखाता है, मैं उस लिंक में एक उत्तर पोस्ट नहीं कर सकता। यह वास्तव में अच्छा है। –