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 library of the Apache Software Foundation. POI provides APIs for Java programs to read and write Microsoft Office files.
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:
View plaincopy to clipboardprint?
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 an 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 data
System. out. println ("id:" + id. getNumericCellValue ());
System. out. println ("name:" + name. getRichStringCellValue ());
System. out. println ("password:" + password. getRichStringCellValue ());
}
}
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 an 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 data
System. out. println ("id:" + id. getNumericCellValue ());
System. out. println ("name:" + name. getRichStringCellValue ());
System. out. println ("password:" + password. getRichStringCellValue ());
}
}
Create an xls file:
View plaincopy to clipboardprint?
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;
/**
* Using 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 worksheet named the default value
HSSFSheet sheet = workbook. createSheet ("first page ");
// HSSFSheet sheet = workbook. createSheet ();
// Create a row at the specified index
HSSFRow row = sheet. createRow (short) 0 );
// Create a cell in the specified index
HSSFCell id = row. createCell (short) 0 );
// Define cells as strings
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
Workbook. write (out );
Out. flush ();
// The operation ends. close the file.
Out. close ();
System. out. println ("file generation..." + fileToWrite );
}
}
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;
/**
* Using 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 worksheet named the default value
HSSFSheet sheet = workbook. createSheet ("first page ");
// HSSFSheet sheet = workbook. createSheet ();
// Create a row at the specified index
HSSFRow row = sheet. createRow (short) 0 );
// Create a cell in the specified index
HSSFCell id = row. createCell (short) 0 );
// Define cells as strings
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
Workbook. write (out );
Out. flush ();
// The operation ends. close the file.
Out. close ();
System. out. println ("file generation..." + fileToWrite );
}
}
Read the word content:
View plaincopy to clipboardprint?
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 a WordExtractor
WordExtractor extractor = new WordExtractor (in );
// Extract the DOC file
String text = extractor. getText ();
System. out. println (text );
}
}
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 a WordExtractor
WordExtractor extractor = new WordExtractor (in );
// Extract the DOC file
String text = extractor. getText ();
System. out. println (text );
}
}
Create a word file:
View plaincopy to clipboardprint?
Package poi.doc;
Import java. io. ByteArrayInputStream;
Import java. io. FileOutputStream;
Import org. apache. poi. poifs. filesystem. POIFSFileSystem;
/**
* Using 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 ();
}
}
Package poi.doc;
Import java. io. ByteArrayInputStream;
Import java. io. FileOutputStream;
Import org. apache. poi. poifs. filesystem. POIFSFileSystem;
/**
* Using 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 ();
}
}
The above are a few small examples. To use more complex applications, you can refer to the poi api.
Author: "tnjun123456"