如何将Excel文件拆分成多个工作表?

时间:2013-08-13 05:54:49

标签: java apache-poi export-to-excel

友  我是Apache POI的新手。我被分配了一个任务,根据Excel文件中的数据将Excel文件(WorkBook)拆分成多个表格。我的任务是,

是否可以将单个Excel工作表拆分为多个Excel工作表?如何将Excel工作表输入Apache POI?到目前为止,我已经练习通过Apache POI创建Excel文件,并且不知道如何将Excel表格输入Apache POI。

任何形式的帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

Tutorial将帮助您使用Apache POI在Excel文件中创建表格

虽然解决方案非常简单。

教程代码向您寻求帮助!

public class CalendarDemo {

    private static final String[] days = {
            "Sunday", "Monday", "Tuesday",
            "Wednesday", "Thursday", "Friday", "Saturday"};

    private static final String[]  months = {
            "January", "February", "March","April", "May", "June","July", "August",
            "September","October", "November", "December"};

    public static void main(String[] args) throws Exception {

        Calendar calendar = Calendar.getInstance();
        boolean xlsx = true;
        for (int i = 0; i < args.length; i++) {
            if(args[i].charAt(0) == '-'){
                xlsx = args[i].equals("-xlsx");
            } else {
              calendar.set(Calendar.YEAR, Integer.parseInt(args[i]));
            }
        }
        int year = calendar.get(Calendar.YEAR);


        //Step #01 Creating Excel WorkBook
        Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook();

        Map<String, CellStyle> styles = createStyles(wb);

        for (int month = 0; month < 12; month++) {
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            //create a sheet for each month

            //Step #02  Creating WorkSheets in WorkBook
            Sheet sheet = wb.createSheet(months[month]);

enter image description here