Advertisement
Guest User

Excel I/O

a guest
Jun 17th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import org.apache.poi.ss.usermodel.Cell;
  2. import org.apache.poi.ss.usermodel.Row;
  3. import org.apache.poi.xssf.usermodel.XSSFSheet;
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.FileOutputStream;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10.  
  11. /**
  12. * Created by IntelliJ IDEA.
  13. * User: LAPD
  14. * Date: 17.6.2018 г.
  15. * Time: 12:36 ч.
  16. */
  17. public class _14ExportToExcel {
  18. public static void main(String[] args) {
  19.  
  20. String resourcePath = "D:\\VBprojects\\JavaAdvanced05.2018\\" +
  21. "07StreamAPI\\recources\\";
  22.  
  23. String inputPath = resourcePath + "07. StudentData.txt";
  24. String ouputPath = resourcePath + "07. StudentData.xlsx";
  25.  
  26. try (BufferedReader bufferedReader =
  27. new BufferedReader(new FileReader(inputPath));
  28. FileOutputStream fileOutputStream = new FileOutputStream(ouputPath)) {
  29.  
  30. XSSFWorkbook workbook = new XSSFWorkbook();
  31.  
  32. XSSFSheet sheet = workbook.createSheet("Student data");
  33.  
  34. int rowNum = 0;
  35. String rowData;
  36. while (null != (rowData = bufferedReader.readLine())) {
  37.  
  38. Row row = sheet.createRow(rowNum++);
  39.  
  40. String[] rowCellsData = rowData.split("\\t");
  41.  
  42. int cellNum = 0;
  43. for (String cellData : rowCellsData) {
  44. Cell cell = row.createCell(cellNum++);
  45.  
  46. cell.setCellValue(cellData);
  47. }
  48. }
  49.  
  50. workbook.write(fileOutputStream);
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement