There are many ways to manipulate Excel using Java, and my http://blog.csdn.net/qq_20545159/article/details/45132041 price has been The following is a simple code that uses JXL to generate an XLS format for Excel.
using JXL to generate Excel files You must first add the Jxl.jar package to the path of your project.
Package com.silence.excel;
Import Java.io.File;
Import JXL. Workbook;
Import Jxl.write.Label;
Import Jxl.write.WritableSheet;
Import Jxl.write.WritableWorkbook;
public class Jxlexcel {
public static void Main (string[] args) {
Set row labels for this table
String[] title = {"id", "name", "Age", "sex", "number"};
Create a file
File File = new file ("E:\\temp.xls");
try {
if (!file.exists ()) {
File.createnewfile ();
}
Create a work
Writableworkbook workbook = workbook.createworkbook (file);
Create sheet
Writablesheet sheet = workbook.createsheet ("Sheet0", 0);
Label label = NULL;
Set a row column name
for (int i = 0; i < title.length; i++) {
Label = new label (i, 0, title[i]);
Sheet.addcell (label);
}
Loop inserts data into each row
for (int i = 0; i < i++) {
Label = new label (0, I, "user" + i);
Sheet.addcell (label);
Label = new label (1, I, "Zhang San" + i);
Sheet.addcell (label);
Label = new label (2, I, +i+ "");
Sheet.addcell (label);
Label = new label (3, I, "a" + i);
Sheet.addcell (label);
Label = new label (4, I, "silencewaking in the Sun" + i);
Sheet.addcell (label);
}
End
Workbook.write ();
Workbook.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Java Read Excel
Package com.silence.excel;
Import Java.io.File;
Import java.io.IOException;
Import JXL. Cell;
Import JXL. Sheet;
Import JXL. Workbook;
Import jxl.read.biff.BiffException;
public class Jxlreadexcel {
public static void Main (string[] args) {
try {
Create Workbook
Workbook Workbook = Workbook.getworkbook (New File ("E:\\temp.xls"));
GET worksheet sheet
Sheet Sheet = workbook.getsheet (0);
for (int i = 0; i < sheet.getrows (); i++) {
for (int j = 0; J < Sheet.getcolumns (); j + +) {
Cell cell = Sheet.getcell (j,i);
System.out.print (cell.getcontents () + "");
}
System.out.println ();
}
Get Data
Workbook.close ();
} catch (Biffexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Java generates Excel and reads Excel example