The jar packages used are as follows (can be downloaded directly to the POI Web site and can be downloaded in the attachment of the article):
Poi-3.9-20121203.jar
Poi-ooxml-3.9-20121203.jar
Poi-ooxml-schemas-3.9-20121203.jar
Xmlbeans-2.3.0.jar
There may be conflicting jar packages that need to be removed if the project Lib exists.
Xbean-2.1.0.jar
The specific code is as follows:
Java code
Package com.mengwx;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.ss.usermodel.Row;
Import Org.apache.poi.ss.usermodel.Sheet;
Import Org.apache.poi.ss.usermodel.Workbook;
Import Org.apache.poi.xssf.usermodel.XSSFWorkbook;
/** * Import and Export Excel file classes * Support 2003 (XLS) and 2007 (XLSX) versions of Excel files * * @author MENGWX/public class Excelforpoi {
public static void Main (string[] args) {//file path String execelfile = "c:/book2007.xlsx";
String execelfile = "C:/book2003.xls";
Import Excel New Operationexcelforpoi (). Impexcel (Execelfile);
Export Excel String Expfilepath = "C:/testbook.xls";
New Operationexcelforpoi (). Expexcel (Expfilepath); /** * Import Excel * @param eXecelfile/public void Impexcel (String execelfile) {try {//Construct Workbook object, Execelfi
Le is the incoming file path (get Excel Workspace) Workbook book = null;
try {//Excel 2007 Get Method book = new Xssfworkbook (new FileInputStream (Execelfile)); catch (Exception ex) {//Excel 2003 get Method book = new Hssfworkbook (New File
InputStream (Execelfile));
///Read the first Sheet page of the table Sheet Sheet = book.getsheetat (0);
Define row, cell row row;
String cell;
Total number of rows, starting from 0 int totalrows = sheet.getlastrownum (); Loop out the contents of the table, first loop out the row, and then take out the column for the row loop (int i = 1; I <= totalrows; i++) {row = Sheet.getr
ow (i);
Process empty row if (row = = null) {continue;
} Total number of columns, starting from 0 int totalcells = row.getlastcellnum (); for (int j = Row.getfirstcellnum (); J < Totalcells J + +) {//Processing empty column if (row.g
Etcell (j) = = null) {continue;
///Get cell contents by Row.getcell (j). ToString () = Row.getcell (j). ToString ();
System.out.print (cell + "T");
} System.out.println ("");
} catch (FileNotFoundException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
} public void Expexcel (String expfilepath) {outputstream OS = null;
Workbook book = null;
try {//output stream OS = new FileOutputStream (Expfilepath); Create Workspace (97-2003) book = new HSSFworkbook ();
Create the first Sheet page Sheet sheet= book.createsheet ("test");
Generates the first line row row = Sheet.createrow (0);
Assigns the first column of the first row to Row.createcell (0). Setcellvalue ("Column1");
Assigns a value to the second column of the first row Row.createcell (1). Setcellvalue ("Column2");
Write file Book.write (OS);
catch (FileNotFoundException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
Finally {//close output stream try {os.close ();
catch (IOException e) {e.printstacktrace (); }
}
}
}