Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import org.apache.poi.ss.usermodel.*;
  2. import org.apache.poi.xssf.usermodel.XSSFRow;
  3. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  4.  
  5. import java.io.FileOutputStream;
  6.  
  7. public class XslxCreator {
  8.  
  9. public static void main(String[] args) {
  10. try{
  11. Workbook wb = new XSSFWorkbook();
  12. CreationHelper ch = wb.getCreationHelper();
  13. Sheet sh = wb.createSheet("mySheet");
  14. CellStyle cs1 = wb.createCellStyle();
  15. CellStyle cs2 = wb.createCellStyle();
  16. DataFormat df = wb.createDataFormat();
  17. Font f1 = wb.createFont();
  18. Font f2 = wb.createFont();
  19. f1.setFontHeight((short)12);
  20. f1.setColor(IndexedColors.RED.getIndex());
  21. f1.setBold(true);
  22. f2.setFontHeight((short)15);
  23. f2.setColor(IndexedColors.BLUE.getIndex());
  24. f2.setItalic(true);
  25. cs1.setFont(f1);
  26. cs1.setDataFormat(df.getFormat("#,##0.0"));
  27. cs2.setFont(f2);
  28. cs2.setDataFormat(df.getFormat("text"));
  29. cs2.setBorderBottom(BorderStyle.THIN);
  30. for(int rown = 0; rown < 10; rown++) {
  31. Row row = sh.createRow(rown);
  32. for(int celln = 0; celln < 10; celln+=2) {
  33. Cell c1 = row.createCell(celln);
  34. c1.setCellStyle(cs1);
  35. Cell c2 = row.createCell(celln + 1);
  36. c2.setCellStyle(cs2);
  37. c1.setCellValue((double)rown + (celln/10));
  38. c2.setCellValue(ch.createRichTextString("Hello! " + celln));
  39. }
  40. }
  41. String fileName = "src\\main\\resources\\xml\\Ex1.xlsx";
  42. FileOutputStream out = new FileOutputStream(fileName);
  43. wb.write(out);
  44. } catch (Exception e) {
  45. System.out.println(e);
  46. }
  47.  
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement