If you want to import Excel data to a database or export the data in the database as Excel, poi is a good choice. Apache poi is an open source code library of the Apache Software Foundation. Poi provides APIs to Java programs for Microsoft
Office Format File Read and Write Functions.
Hssf-provides the ability to read and write Microsoft Excel files.
Xssf-provides the ability to read and write files in Microsoft Excel ooxml format.
Hwpf-read and write Microsoft Word files.
Hslf-supports reading and writing Microsoft PowerPoint files.
Hdgf-read and write Microsoft Visio files.
The following two demos read and write Excel and Word respectively:
Read XLS data:
Package poi.xls; import Org. apache. poi. hssf. usermodel. hssfworkbook; import Org. apache. poi. hssf. usermodel. hssfsheet; import Org. apache. poi. hssf. usermodel. hssfrow; import Org. apache. poi. hssf. usermodel. hssfcell; import Java. io. fileinputstream;/*** use poi to read content from Excel */public class xlsreader {public static string filetoread = "C:/test.xls "; public static void main (string ARGs []) throws exception {// create a reference to the Excel Workbook file hssfworkbook workbook = new hssfworkbook (New fileinputstream (filetoread )); // hssfsheet sheet = workbook. getsheet ("first page"); hssfsheet sheet = workbook. getsheetat (0); // read the value of the specified index row hssfrow ROW = sheet. getrow (1); // read the value of the specified index. hssfcell id = row. getcell (short) 0); hssfcell name = row. getcell (short) 1); hssfcell Password = row. getcell (short) 2); // read the data system. out. println ("ID:" + id. getnumericcellvalue (); system. out. println ("name:" + name. getrichstringcellvalue (); system. out. println ("Password:" + password. getrichstringcellvalue ());}}
Create an xls file:
Package poi.xls; import Java. io. fileoutputstream; import Org. apache. poi. hssf. usermodel. hssfrichtextstring; import Org. apache. poi. hssf. usermodel. hssfworkbook; import Org. apache. poi. hssf. usermodel. hssfsheet; import Org. apache. poi. hssf. usermodel. hssfrow; import Org. apache. poi. hssf. usermodel. hssfcell;/*** use poi to write content to excel */public class xlswriter {public static string filetowrite = "C:/test.xls "; public static void main (string [] ARGs) throws exception {// create a new Excel Workbook hssfworkbook workbook = new hssfworkbook (); // create a worksheet in an Excel Workbook, its name is hssfsheet = workbook. createsheet ("Page 1"); // hssfsheet sheet = workbook. createsheet (); // create a row in the specified index. hssfrow ROW = sheet. createrow (short) 0); // create the cell hssfcell id = row in the specified index. createcell (short) 0); // defines the cell as the string type ID. setcelltype (hssfcell. cell_type_numeric); // enter some content in the cell. hssfrichtextstring can solve the garbled problem. hssfrichtextstring idcontent = new hssfrichtextstring ("User ID"); Id. setcellvalue (idcontent); hssfcell name = row. createcell (short) 1); Name. setcelltype (hssfcell. cell_type_string); hssfrichtextstring namecontent = new hssfrichtextstring ("username"); Name. setcellvalue (namecontent); hssfcell Password = row. createcell (short) 2); password. setcelltype (hssfcell. cell_type_string); hssfrichtextstring passwordcontent = new hssfrichtextstring ("User Password"); password. setcellvalue (passwordcontent); // create an output file stream fileoutputstream out = new fileoutputstream (filetowrite); // Save the corresponding Excel Workbook to the workbook. write (out); out. flush (); // The operation ends. close the file out. close (); system. out. println ("file generation... "+ filetowrite );}}
Read the word content:
Package poi.doc;/*** use poi to read content from word */import Java. io. fileinputstream; import Org. apache. poi. hwpf. extractor. wordextractor; public class docreader {public static string filetoread = "C:/test.doc"; public static void main (string [] ARGs) throws exception {// create an input stream to read the DOC file fileinputstream in = new fileinputstream (filetoread); // create wordextractor extractor = new wordextractor (in ); // extract string text = extractor from the doc file. gettext (); system. out. println (text) ;}}// create a Word file: Package poi.doc; import Java. io. bytearrayinputstream; import Java. io. fileoutputstream; import Org. apache. poi. poifs. filesystem. poifsfilesystem;/*** use poi to write content to word */public class docwriter {public static string filetowrite = "C:/test.doc "; public static void main (string [] ARGs) throws exception {string content = "test data, will be written to the document"; byte B [] = content. getbytes (); bytearrayinputstream BAIS = new bytearrayinputstream (B); poifsfilesystem FS = new poifsfilesystem (); fileoutputstream ostream = new fileoutputstream (filetowrite); FS. writefilesystem (ostream); BAIS. close (); ostream. close ();}}