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

标签 java file apache-poi xwpf

我正在尝试使用 java 在 ms word 文件中创建任何一年的日历。请您帮助我。提前致谢。

最佳答案

以下是在 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-schemas-3.11.jar

xmlbeans-2.6.jar

关于java - 使用java在ms word文件中创建任何一年的日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28234494/

相关文章:

excel - 使用 ColdFusion 对 Excel 文件进行条件格式化

java - Maven 无法找到插件描述符

Java:在 for-each 循环中删除列表中的记录时出现异常

json - 读入json文件,写入不缩进

c - 使用文件的管道输入,无法获取通过管道传递给它的文件名而不是文件内容

java - 使用 HSSF (Apache POI) 复制并粘贴行

java - POI Java - 带小数的数字单元格

java - Eclipse 中的内存不足错误

java - 安卓 N : Platform Migration toward OpenJDK 8

c++ - 将 url 存储到文件中,以便可以快速访问它们