ro.dabuno.office.integration.Xlsx2Word.java Source code

Java tutorial

Introduction

Here is the source code for ro.dabuno.office.integration.Xlsx2Word.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ro.dabuno.office.integration;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

/**
 *
 * @author bogdan
 */
public class Xlsx2Word {

    private static final Logger log = LogManager.getLogger(Xlsx2Word.class);

    public static String test = "123";

    public static void main(String[] args) throws Exception {
        log.info("starting app");
        //        Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
        Workbook wb = new XSSFWorkbook(new FileInputStream("office-files/Input.xlsx"));

        DataFormatter formatter = new DataFormatter();

        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet sheet = wb.getSheetAt(i);
            System.out.println(wb.getSheetName(i));
            int j = 4;
            for (Row row : sheet) {
                System.out.println("rownum: " + row.getRowNum());
                for (Cell cell : row) {
                    CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
                    System.out.print(cellRef.formatAsString());
                    System.out.print(" - ");
                    // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
                    String text = formatter.formatCellValue(cell);
                    System.out.println(text);

                    System.out.println("------------");
                    // Alternatively, get the value and format it yourself
                    switch (cell.getCellTypeEnum()) {
                    case STRING:
                        System.out.println(cell.getRichStringCellValue().getString());
                        break;
                    case NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {
                            System.out.println(cell.getDateCellValue());
                        } else {
                            System.out.print(cellRef.formatAsString());
                            System.out.print(" - ");
                            System.out.println((long) cell.getNumericCellValue());
                        }
                        break;
                    case BOOLEAN:
                        System.out.println(cell.getBooleanCellValue());
                        break;
                    case FORMULA:
                        System.out.println(cell.getCellFormula());
                        break;
                    case BLANK:
                        System.out.println();
                        break;
                    default:
                        System.out.println();
                    }

                }
                j--;
                if (j == 0) {
                    break;
                }
            }
        }

        XWPFDocument doc = new XWPFDocument();

        XWPFParagraph p0 = doc.createParagraph();
        XWPFRun r0 = p0.createRun();
        r0.setBold(false);
        r0.setText("Domnule");
        XWPFRun r00 = p0.createRun();
        r00.setBold(true);
        r00.setText(" Ionescu Ion");

        FileOutputStream out = new FileOutputStream("out/xlsx2word.docx");
        doc.write(out);
        out.close();
    }
}