Apache POI - how to create new and read Microsoft Excel Document with Eclipse

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

ความคิดเห็น •

  • @helinchooi6741
    @helinchooi6741 5 ปีที่แล้ว +1

    Nice Tutorial!

  • @b.o.t7888
    @b.o.t7888 5 ปีที่แล้ว

    Please send the link of the code

    • @javaeetips868
      @javaeetips868  5 ปีที่แล้ว

      Create a normal java project then convert to maven project. Replace pom.xml
      4.0.0
      WordDemo
      WordDemo
      0.0.1-SNAPSHOT

      org.apache.poi
      poi-ooxml
      4.1.0

      src

      maven-compiler-plugin
      3.3
      1.8
      1.8


      Here's the code:
      package tip.javaee;
      import java.io.FileOutputStream;
      import java.math.BigInteger;
      import org.apache.poi.xwpf.usermodel.XWPFAbstractNum;
      import org.apache.poi.xwpf.usermodel.XWPFDocument;
      import org.apache.poi.xwpf.usermodel.XWPFNumbering;
      import org.apache.poi.xwpf.usermodel.XWPFParagraph;
      import org.apache.poi.xwpf.usermodel.XWPFRun;
      import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
      import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
      public class MultiLevelNumbering {
      private static int subIndex = 1;
      private static int mainIndex = 1;
      public static void main(String[] args) throws Exception {
      StringBuilder xml = new StringBuilder();
      xml.append(
      "");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(
      " ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append(" ");
      xml.append("");
      XWPFDocument document = new XWPFDocument();
      XWPFParagraph paragraph = document.createParagraph();
      XWPFRun run = paragraph.createRun();
      CTNumbering cTNumbering = CTNumbering.Factory.parse(xml.toString());
      CTAbstractNum cTAbstractNum = cTNumbering.getAbstractNumArray(0);
      XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
      XWPFNumbering numbering = document.createNumbering();
      BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
      BigInteger numID = numbering.addNum(abstractNumID);
      int i = 1;
      createItem(paragraph, document, numID, run, i, false);
      createItem(paragraph, document, numID, run, i, true);
      createItem(paragraph, document, numID, run, i, true);
      createItem(paragraph, document, numID, run, i, false);
      createItem(paragraph, document, numID, run, i, true);
      paragraph = document.createParagraph();
      run = paragraph.createRun();
      String filename = "c:/test/test.docx";
      FileOutputStream out = new FileOutputStream(filename);
      document.write(out);
      System.out.println(filename + " has been written successully");
      }
      public static void createItem(XWPFParagraph paragraph, XWPFDocument document, BigInteger numID, XWPFRun run,
      int index, boolean isSub) {
      String text = "Main item ";
      paragraph = document.createParagraph();
      paragraph.setNumID(numID);
      if (isSub) {
      text = "Sub item ";
      paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1));
      }
      run = paragraph.createRun();
      if (isSub) {
      run.setText(text + subIndex++);
      } else {
      run.setText(text + mainIndex++);
      }
      }
      }

  • @iremsaltoglu5998
    @iremsaltoglu5998 4 ปีที่แล้ว

    pls share the code, this is word processing not this videos code