Java POI Import Export Excel xsl XSLX

Source: Internet
Author: User
Tags xsl

  1. Import Java.io.FileInputStream;
  2. Import Java.io.FileOutputStream;
  3. Import java.io.IOException;
  4. Import Java.io.InputStream;
  5. Import Java.io.OutputStream;
  6. Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
  7. Import Org.apache.poi.ss.usermodel.Cell;
  8. Import Org.apache.poi.ss.usermodel.Row;
  9. Import Org.apache.poi.ss.usermodel.Sheet;
  10. Import Org.apache.poi.ss.usermodel.Workbook;
  11. Import Org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. Public class FFF {
  13. public static void Main (string[] args) throws IOException {
  14. String path = "d:/";
  15. String fileName = "Test";
  16. String FileType = "xlsx";
  17. Writer (path, fileName, FileType);
  18. Read (path, fileName, FileType);
  19. }
  20. private static void writer (string path, String filename,string fileType) throws IOException {
  21. //Create Work Document Object
  22. Workbook WB = null;
  23. if (filetype.equals ("xls")) {
  24. WB = new Hssfworkbook ();
  25. }
  26. Else if (filetype.equals ("xlsx"))
  27. {
  28. WB = new Xssfworkbook ();
  29. }
  30. Else
  31. {
  32. System.out.println ("Your document is not formatted correctly!")    ");
  33. }
  34. //Create sheet Object
  35. Sheet Sheet1 = (Sheet) wb.createsheet ("Sheet1");
  36. //Loop write row data
  37. For (int i = 0; i < 5; i++) {
  38. Row row = (row) sheet1.createrow (i);
  39. //Loop write column data
  40. For (int j = 0; j < 8; j + +) {
  41. Cell cell = Row.createcell (j);
  42. Cell.setcellvalue ("test" +j);
  43. }
  44. }
  45. //Create a file stream
  46. OutputStream stream = new FileOutputStream (path+filename+".")    +filetype);
  47. //Write Data
  48. Wb.write (stream);
  49. //Close file stream
  50. Stream.Close ();
  51. }
  52. public static void Read (String path,string filename,string fileType) throws IOException
  53. {
  54. InputStream stream = new FileInputStream (path+filename+".")    +filetype);
  55. Workbook WB = null;
  56. if (filetype.equals ("xls")) {
  57. WB = New Hssfworkbook (stream);
  58. }
  59. Else if (filetype.equals ("xlsx")) {
  60. WB = New Xssfworkbook (stream);
  61. }
  62. else {
  63. System.out.println ("The Excel format you entered is incorrect");
  64. }
  65. Sheet Sheet1 = Wb.getsheetat (0);
  66. For (Row row:sheet1) {
  67. For (Cell cell:row) {
  68. System.out.print (Cell.getstringcellvalue () +"");
  69. }
  70. System.out.println ();
  71. }
  72. }
  73. }

Java POI Import Export Excel xsl XSLX

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.