私はそれらの中に異なるシートを持つ3つ以上のExcelファイルを持っています。新しいファイルにシートをコピーして新しい空のファイルを作成し、必要な順序で配置する必要がありますそれぞれの書式にデータを記入することができます。ファイル間のシートのクローニング
ジャカルタPOI(XSSFWorkbook
)を使用してこれを行うにはどうすればよいですか?
私はそれらの中に異なるシートを持つ3つ以上のExcelファイルを持っています。新しいファイルにシートをコピーして新しい空のファイルを作成し、必要な順序で配置する必要がありますそれぞれの書式にデータを記入することができます。ファイル間のシートのクローニング
ジャカルタPOI(XSSFWorkbook
)を使用してこれを行うにはどうすればよいですか?
まずアップ、私はあなたがApache POIを意味だと思う - それは別のワークブックからコピーシートの面では...今かなりの数年間、
をApacheのジャカルタPOIされていない、それは、行うことができます何らかのコーディングが必要になります。まず、使用しているセルスタイルを特定し、それらを複製したいと思うでしょう。あなたが再作成を続行したくない場合や、限界に達するまで、どのセルスタイルがどのセルスタイルに移動するのかを確認してください。 CellStyle.cloneStyleFrom(CellStyle)はあなたが望む方法です。
次に、ソースシートごとに、対象ワークブックにシートを作成します。すべてのソース行をループし、新しいターゲット行を作成します。次に、セルをループし、セルの種類によって切り替えて、適切な値を取得して設定します。リンスとリピート!
FileOutputStream os =新しいFileOutputStream( "differnetFileName") readWorkbook.write(os);
OSに書き込み操作を利用できると思いますが、diffファイル名で動作します。
これは、ブックを別のブックから別のブックにコピーする実装です。私はGagravarrの記述どおりにすべてをやった。このソリューションは私のために働く。このコードは、シートにテーブルなどがない場合に機能します。シートにシンプルなテキスト(String、boolean、intなど)、数式が含まれている場合、このソリューションは機能します。
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();