Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. public class WordExtractor {
  2. public static void main(String[] args) {
  3. try {
  4. File inputFile = new File("table.docx");
  5. POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
  6.  
  7. String text = extractor.getText();
  8. BufferedReader reader = new BufferedReader(new StringReader(text));
  9. String line = null;
  10. boolean breakRead = false;
  11. int rowCount = 0;
  12. HSSFWorkbook workbook = new HSSFWorkbook();
  13. HSSFSheet sheet = workbook.createSheet("sheet1");
  14. while (!breakRead) {
  15. line = reader.readLine();
  16. if (line != null) {
  17. Row row = sheet.createRow(rowCount);
  18. StringTokenizer st = new StringTokenizer(line, "t");
  19. int cellnum = 0;
  20. while (st.hasMoreTokens()) {
  21. Cell cell = row.createCell(cellnum++);
  22. String token = st.nextToken();
  23. System.out.println(" = " + token);
  24. cell.setCellValue(token);
  25. }
  26. } else {
  27. breakRead = true;
  28. }
  29. rowCount++;
  30. }
  31.  
  32. try {
  33. FileOutputStream out =
  34. new FileOutputStream(new File("new.xls"));
  35. workbook.write(out);
  36. out.close();
  37. } catch (FileNotFoundException e) {
  38. e.printStackTrace();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. } catch (Exception ex) {
  43. ex.printStackTrace();
  44. }
  45. }
  46. }
  47.  
  48. public class WordExtractor {
  49. public static void main(String[] args) {
  50. try {
  51. File inputFile = new File("table.docx");
  52. POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
  53. String text = extractor.getText();
  54. BufferedReader reader = new BufferedReader(new StringReader(text));
  55. String line = null;
  56. boolean breakRead = false;
  57. int rowCount = 0;
  58. HSSFWorkbook workbook = new HSSFWorkbook();
  59. HSSFSheet sheet = workbook.createSheet("sheet1");
  60. while (!breakRead) {
  61. line = reader.readLine();
  62. if (line != null) {
  63. Row row = sheet.createRow(rowCount);
  64. StringTokenizer st = new StringTokenizer(line, "t");
  65. int cellnum = 0;
  66. while (st.hasMoreTokens()) {
  67. Cell cell = row.createCell(cellnum++);
  68. String token = st.nextToken();
  69. cell.setCellValue(token);
  70. }
  71. } else {
  72. breakRead = true;
  73. }
  74. rowCount++;
  75. if (rowCount % 100 == 0) {
  76. // breakRead = true;
  77. System.gc();
  78. }
  79. }
  80. reader.close();
  81. extractor.close();
  82. System.gc();
  83. try {
  84. FileOutputStream out =
  85. new FileOutputStream(new File("new.xls"));
  86. workbook.write(out);
  87. out.close();
  88. System.out.println("Excel written successfully..");
  89.  
  90. } catch (FileNotFoundException e) {
  91. e.printStackTrace();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. } catch (Exception ex) {
  96. ex.printStackTrace();
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement