使用java在ms word文件中创建任何年份的日历

时间:2015-01-30 11:04:01

标签: java file apache-poi xwpf

我正在尝试使用java创建ms文件中任何一年的日历。请你帮我解决。谢谢。

1 个答案:

答案 0 :(得分:1)

以下是在docx文件中编写特定年份日历的代码

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Scanner;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class MyCalendar {

    //Blank Document
      static XWPFDocument document= new XWPFDocument();

    public static void main(String[] args) {


        // represents the year
        int year;

        // ask year from user
        Scanner in = new Scanner(System.in);
        System.out.print("Enter year: ");

        // read them as string
        String yearText = in.next();

        in.close();

        try {


               //Write the Document in file system
               FileOutputStream out = new FileOutputStream(new File("Year "+yearText+".docx"));

            // throws NumberFormatException if not convertible.
            // It would be caught below:
            year = Integer.parseInt(yearText);

            // print the calendar for the given year.
            for(int i=1; i<=12; i++){
                printCalendarMonthYear(i, year);
            }

            document.write(out);
            out.close();

        } catch (NumberFormatException e) {
            // handles NumberFormatException
            System.err.println("Numberat Error: " + e.getMessage());
        } catch (Exception e) {
            // handles any other Exception
            System.err.println(e.getMessage());
        }
    }

    /*
     * prints a calendar month based on month / year info
     */
    private static void printCalendarMonthYear(int month, int year) {
        // create a new GregorianCalendar object
        Calendar cal = new GregorianCalendar();

        // set its date to the first day of the month/year given by user
        cal.clear();
        cal.set(year, month - 1, 1);

        document.createParagraph().createRun().setText(""+cal.getDisplayName(Calendar.MONTH, Calendar.LONG,
                        Locale.US) + " " + cal.get(Calendar.YEAR));

        // obtain the weekday of the first day of month.
        int firstWeekdayOfMonth = cal.get(Calendar.DAY_OF_WEEK);

        // obtain the number of days in month.
        int numberOfMonthDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        // print anonymous calendar month based on the weekday of the first
        // day of the month and the number of days in month:
        printCalendar(numberOfMonthDays, firstWeekdayOfMonth-1);
    }

    /*
     *  prints an anonymous calendar month based on the weekday of the first
     *  day of the month and the number of days in month:
     */
    private static void printCalendar(int numberOfMonthDays, int firstWeekdayOfMonth) {

        // reset index of weekday
        int weekdayIndex = 0;

        // print calendar weekday header
        XWPFTable table = document.createTable();
        //create first row
        XWPFTableRow tableRowOne = table.getRow(0);
        tableRowOne.getCell(0).setText("Su");
        tableRowOne.addNewTableCell().setText("Mo");
        tableRowOne.addNewTableCell().setText("Tu");
        tableRowOne.addNewTableCell().setText("We");
        tableRowOne.addNewTableCell().setText("Th");
        tableRowOne.addNewTableCell().setText("Fr");
        tableRowOne.addNewTableCell().setText("Sa");

        // leave/skip weekdays before the first day of month
        XWPFTableRow tableRow = table.createRow();
        int ind = 0;
        for (int day = 1; day <= firstWeekdayOfMonth; day++) {
            ind = day;
            tableRow.getCell(ind).setText(" ");
            weekdayIndex++;
        }

        // print the days of month in tabular format.
        for (int day = 1; day <= numberOfMonthDays; day++) {
            // print day

            tableRow.getCell(ind).setText(""+day);

            // next weekday
            weekdayIndex++;
            // if it is the last weekday
            if (weekdayIndex == 7) {
                // reset it
                weekdayIndex = 0;
                // and go to next line

                tableRow = table.createRow();
                ind = 0;
            }
            else { // otherwise
                // print space
                ind++;
            }
        }

        // print a final new-line.
        document.createParagraph().createRun().addBreak();
    }
}

这将创建年份名称为

的docx文件

您需要以下jar文件才能运行此代码

POI-3.11.jar

POI-OOXML-3.11.jar

POI-OOXML-架构 - 3.11.jar

的xmlbeans-2.6.jar