Java tutorial
/* * Copyright 2014,2015 agwlvssainokuni * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cherry.goods.excel; import java.io.Closeable; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * Microsoft Excel?<br /> */ public class ExcelWriter implements Closeable { /** ??Microsoft Excel */ private final Workbook workbook; /** ?? */ private Sheet currentSheet; /** ??? */ private int rownum; /** * Microsoft Excel???<br /> * ?????????{@link #setCurrentSheet(int)}??? * * @param workbook ??Microsoft Excel */ public ExcelWriter(Workbook workbook) { this.workbook = workbook; setCurrentSheet(this.workbook.getActiveSheetIndex()); } /** * @return ??Microsoft Excel?? */ public int getNumberOfSheets() { return workbook.getNumberOfSheets(); } /** * @param sheetIndex ? * @return ??????? */ public String getSheetName(int sheetIndex) { return workbook.getSheetName(sheetIndex); } /** * ???<br /> * * @param sheetIndex ????? */ public void setCurrentSheet(int sheetIndex) { currentSheet = workbook.getSheetAt(sheetIndex); rownum = 0; } /** * ?? (?) ??<br /> * * @param offsetRows ??? (???) */ public void skipRows(int offsetRows) { rownum += offsetRows; } /** * ?<br /> * ???1(1) * * @param record 1(1) */ public void write(String... record) { write(0, record); } /** * ?<br /> * ???1(1) * * @param offsetCols ????? (???) * @param record 1(1) */ public void write(int offsetCols, String... record) { Row row = currentSheet.getRow(rownum); if (row == null) { row = currentSheet.createRow(rownum); } rownum += 1; for (int i = 0; i < record.length; i++) { int colnum = i + offsetCols; Cell cell = row.getCell(colnum); if (record[i] == null) { if (cell != null) { row.removeCell(cell); } } else { if (cell == null) { cell = row.createCell(colnum); } cell.setCellValue(record[i]); } } } /** * Microsoft Excel??<br /> * ?????? */ @Override public void close() { // NOTHING } }