de.fionera.javamailer.dataProcessors.parseFilesForImport.java Source code

Java tutorial

Introduction

Here is the source code for de.fionera.javamailer.dataProcessors.parseFilesForImport.java

Source

package de.fionera.javamailer.dataProcessors;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.converter.WordToHtmlUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.Document;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by Tim Windelschmidt (fionera)
 * See LICENSE for Copyright
 */
public class parseFilesForImport {
    private String[] headers;
    private String[][] data;

    /**
     * Gets a XLS file and parse it
     * @param file The XLS File that you want to get parsed
     * @return A ArrayList where the first object is a Array containing the Data and the Second the Header
     */
    public ArrayList<Object> parseXLSFile(File file) {
        int index = -1;
        HSSFWorkbook workbook = null;
        try {
            try {
                FileInputStream inputStream = new FileInputStream(file);
                workbook = new HSSFWorkbook(inputStream);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            assert workbook != null;
            String[] strings = new String[workbook.getNumberOfSheets()];
            //get all sheet names from selected workbook
            for (int i = 0; i < strings.length; i++) {
                strings[i] = workbook.getSheetName(i);
            }
            JFrame frame = new JFrame("Input Dialog");

            String selectedsheet = (String) JOptionPane.showInputDialog(frame,
                    "Which worksheet you want to import ?", "Select Worksheet", JOptionPane.QUESTION_MESSAGE, null,
                    strings, strings[0]);

            if (selectedsheet != null) {
                for (int i = 0; i < strings.length; i++) {
                    if (workbook.getSheetName(i).equalsIgnoreCase(selectedsheet))
                        index = i;
                }
                HSSFSheet sheet = workbook.getSheetAt(index);
                HSSFRow row = sheet.getRow(0);

                if (row != null) {
                    headers = new String[row.getLastCellNum()];

                    for (int i = 0; i < row.getLastCellNum(); i++) {
                        headers[i] = row.getCell(i).toString();
                    }
                }

                data = new String[sheet.getLastRowNum()][];
                for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
                    row = sheet.getRow(j);
                    int rowCount = row.getLastCellNum();
                    String[] dataRow = new String[rowCount];
                    for (int i = 0; i < rowCount; i++) {
                        HSSFCell cell = row.getCell(i, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK);
                        dataRow[i] = cell.toString();
                    }
                    data[j - 1] = dataRow;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Object> returnData = new ArrayList<>();
        returnData.add(data);
        returnData.add(headers);

        return returnData;
    }

    /**
     * Gets a XLSX file and parse it
     * @param file The XLSX File that you want to get parsed
     * @return A ArrayList where the first object is a Array containing the Data and the Second the Header
     */
    public ArrayList<Object> parseXLSXFile(File file) {
        int index = -1;
        XSSFWorkbook workbook = null;
        try {
            try {
                FileInputStream inputStream = new FileInputStream(file);
                workbook = new XSSFWorkbook(inputStream);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            assert workbook != null;
            String[] strings = new String[workbook.getNumberOfSheets()];
            //get all sheet names from selected workbook
            for (int i = 0; i < strings.length; i++) {
                strings[i] = workbook.getSheetName(i);
            }
            JFrame frame = new JFrame("Input Dialog");

            String selectedsheet = (String) JOptionPane.showInputDialog(frame,
                    "Which worksheet you want to import ?", "Select Worksheet", JOptionPane.QUESTION_MESSAGE, null,
                    strings, strings[0]);

            if (selectedsheet != null) {
                for (int i = 0; i < strings.length; i++) {
                    if (workbook.getSheetName(i).equalsIgnoreCase(selectedsheet))
                        index = i;
                }
                XSSFSheet sheet = workbook.getSheetAt(index);
                XSSFRow row = sheet.getRow(0);

                if (row != null) {
                    headers = new String[row.getLastCellNum()];

                    for (int i = 0; i < row.getLastCellNum(); i++) {
                        headers[i] = row.getCell(i).toString();
                    }
                }

                data = new String[sheet.getLastRowNum()][];
                for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
                    row = sheet.getRow(j);
                    int rowCount = row.getLastCellNum();
                    String[] dataRow = new String[rowCount];
                    for (int i = 0; i < rowCount; i++) {
                        XSSFCell cell = row.getCell(i, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK);
                        dataRow[i] = cell.toString();
                    }
                    data[j - 1] = dataRow;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Object> returnData = new ArrayList<>();
        returnData.add(data);
        returnData.add(headers);

        return returnData;
    }

    /**
     * Gets a DOC file and parse it to HTML
     * @param file The DOC File that you want to get parsed
     * @return A String congaing the DOC Document converted to HTML
     */
    public String parseDOCFile(File file) {

        HWPFDocumentCore wordDocument;
        String result = "";
        DOMSource domSource;
        StreamResult streamResult;
        ByteArrayOutputStream out;
        Document htmlDocument;
        WordToHtmlConverter wordToHtmlConverter;

        try {
            wordDocument = WordToHtmlUtils.loadDoc(file);

            wordToHtmlConverter = new WordToHtmlConverter(
                    DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
            wordToHtmlConverter.processDocument(wordDocument);
            htmlDocument = wordToHtmlConverter.getDocument();
            domSource = new DOMSource(htmlDocument);
            out = new ByteArrayOutputStream();
            streamResult = new StreamResult(out);

            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer serializer = tf.newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.METHOD, "html");
            serializer.transform(domSource, streamResult);

            out.close();

            result = new String(out.toByteArray());

        } catch (IOException | ParserConfigurationException | TransformerException e) {
            e.printStackTrace();
        }

        return result;
    }
}