Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WordExtractor {
- public static void main(String[] args) {
- try {
- File inputFile = new File("table.docx");
- POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
- String text = extractor.getText();
- BufferedReader reader = new BufferedReader(new StringReader(text));
- String line = null;
- boolean breakRead = false;
- int rowCount = 0;
- HSSFWorkbook workbook = new HSSFWorkbook();
- HSSFSheet sheet = workbook.createSheet("sheet1");
- while (!breakRead) {
- line = reader.readLine();
- if (line != null) {
- Row row = sheet.createRow(rowCount);
- StringTokenizer st = new StringTokenizer(line, "t");
- int cellnum = 0;
- while (st.hasMoreTokens()) {
- Cell cell = row.createCell(cellnum++);
- String token = st.nextToken();
- System.out.println(" = " + token);
- cell.setCellValue(token);
- }
- } else {
- breakRead = true;
- }
- rowCount++;
- }
- try {
- FileOutputStream out =
- new FileOutputStream(new File("new.xls"));
- workbook.write(out);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- public class WordExtractor {
- public static void main(String[] args) {
- try {
- File inputFile = new File("table.docx");
- POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
- String text = extractor.getText();
- BufferedReader reader = new BufferedReader(new StringReader(text));
- String line = null;
- boolean breakRead = false;
- int rowCount = 0;
- HSSFWorkbook workbook = new HSSFWorkbook();
- HSSFSheet sheet = workbook.createSheet("sheet1");
- while (!breakRead) {
- line = reader.readLine();
- if (line != null) {
- Row row = sheet.createRow(rowCount);
- StringTokenizer st = new StringTokenizer(line, "t");
- int cellnum = 0;
- while (st.hasMoreTokens()) {
- Cell cell = row.createCell(cellnum++);
- String token = st.nextToken();
- cell.setCellValue(token);
- }
- } else {
- breakRead = true;
- }
- rowCount++;
- if (rowCount % 100 == 0) {
- // breakRead = true;
- System.gc();
- }
- }
- reader.close();
- extractor.close();
- System.gc();
- try {
- FileOutputStream out =
- new FileOutputStream(new File("new.xls"));
- workbook.write(out);
- out.close();
- System.out.println("Excel written successfully..");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement