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.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Microsoft Excel<br /> */ public class ExcelFactory { /** * Excel??<br /> * ???????? * * @param in ? * @return Excel * @throws InvalidFormatException ? * @throws IOException ? */ public static Workbook open(InputStream in) throws InvalidFormatException, IOException { return WorkbookFactory.create(in); } /** * Excel<br /> * ????????? * * @param file ? * @return Excel * @throws InvalidFormatException ? * @throws FileNotFoundException ????? * @throws IOException ? */ public static Workbook load(File file) throws InvalidFormatException, FileNotFoundException, IOException { try (InputStream in = new FileInputStream(file)) { return WorkbookFactory.create(in); } } /** * Excel (.xls) ???<br /> * * @return ???Excel */ public static Workbook createBlankXls() { return createSheet(new HSSFWorkbook()); } /** * Excel (.xls) ???<br /> * * @param sheetname ?????? * @return ???Excel */ public static Workbook createBlankXls(String sheetname) { return createSheet(new HSSFWorkbook(), sheetname); } /** * Excel (.xlsx) ???<br /> * * @return ???Excel */ public static Workbook createBlankXlsx() { return createSheet(new XSSFWorkbook()); } /** * Excel (.xlsx) ???<br /> * * @param sheetname ?????? * @return ???Excel */ public static Workbook createBlankXlsx(String sheetname) { return createSheet(new XSSFWorkbook(), sheetname); } /** * ???<br /> * * @param workbook ??Excel * @return ???Excel */ private static Workbook createSheet(Workbook workbook) { workbook.createSheet(); return workbook; } /** * ???<br /> * * @param workbook ??Excel * @param sheetname ?????? * @return ???Excel */ private static Workbook createSheet(Workbook workbook, String sheetname) { workbook.createSheet(sheetname); return workbook; } }