At present, more commonly used to achieve Java import, export Excel, there are two technologies Java and Jakarta POI Excel
Directly on the code:
One, POI
POI is an apache project, to be Microsoft Word Excel, Ppt operate, including office2003 and 2007, Excl2003 and 2007. poi now has been updated. So now the mainstream use POI.
xls:
pom:
org.apache.poi
poi-OOXML
3.9
commons-io
commons-io
2.2
Export:
public class PoiCreateExcel {
public static void main(String[] args) {
// Create the header
String[] title = {"id","name","sex"};
// Create an Excel workbook
HSSFWorkbook workbook = new HSSFWorkbook();
// Create a worksheet sheet
HSSFSheet sheet = workbook.createSheet();
// Create the first line
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
// insert the first row
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
// append data
for (int i = 1; i <10; i ++) {// int start here is the second line is a
HSSFRow nexTrow = sheet.createRow(i);
HSSFCell cell2 = nexTrow.createCell(0);
cell2.setCellValue("a"+i);
cell2 = nexTrow.createCell(1);
cell2.setCellValue("user");
cell2 = nexTrow.createCell(2);
cell2.setCellValue("男");
}
// Create a file
File file = new File("d:/poi.xls");
try {
file.createNewFile();
// save content
FileOutputStream stream = FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
Import:
public class PoiReadExcel {
public static void main(String[] args) {
// introduction to parse file
File file = new File("d:/poi.xls");
try {
// Create Excel reads the contents of the file
HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
/**
* The first page reads Sheet
*/
// HSSFSheet sheet = workbook.getSheet("Sheet0");
/**
* The second way to read Sheet page
*/
HSSFSheet sheet = workbook.getSheetAt(0);
int firstRowNum = 0; // start line, line 0
int lasrRowNum = sheet.getLastRowNum (); // read until the last line
for (int i = 0; i < lasrRowNum; i++) {
HSSFRow row = sheet.getRow(i);
// Get the last cell in the current column number
int lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
HSSFCell cell = row.getCell(j);
String value = cell.getStringCellValue (); // Note If Excel which values are String then getStringCellValue if other type you need to change!
System.out.print(value + " ");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace ();
}
}
}
xlsx:
pom:
org.apache.poi
poi-examples
3.9
org.apache.poi
then-excelant
3.9
org.apache.poi
poi-OOXML
3.9
org.apache.poi
poi-OOXML schemas
3.9
org.apache.poi
then-scratchpad
3.9
Export:
public class PoiCreateExcel {
public static void main(String[] args) {
// Create the header
String[] title = {"id","name","sex"};
// Create an Excel workbook
XSSFWorkbook workbook = new XSSFWorkbook();
// Create a worksheet shheet
Sheet sheet = workbook.createSheet();
// Create the first line
Row row = sheet.createRow(0);
Cell cell = null;
// insert the first row
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
// append data
for (int i = 1; i <10; i ++) {// int start here is the second line is a
Row nexTrow = sheet.createRow(i);
Cell cell2 = nexTrow.createCell(0);
cell2.setCellValue("a"+i);
cell2 = nexTrow.createCell(1);
cell2.setCellValue("user");
cell2 = nexTrow.createCell(2);
cell2.setCellValue("男");
}
// Create a file
File file = new File ( "d: /poi.xlsx"); // herein may be modified to higher versions
try {
file.createNewFile();
// The contents of the tray forex bonus activities
FileOutputStream stream = FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
Import:
public class PoiReadExcel {
public List readExcels(InputStream is)throws Exception{
List xlsxList = new ArrayList();
try {
if(is ==null){
throw new IOException ( "file is incorrect!");
}
Workbook workbook = WorkbookFactory.create(is);
FormulaEvaluator fe = workbook.getCreationHelper().createFormulaEvaluator();
// Get the first table
Sheet sheet = workbook.getSheetAt(0);
if(sheet == null){
throw new IOException ( "incoming excel in the first table is empty!");
}
for(int rowNum = 0;rowNum <= sheet.getLastRowNum(); rowNum++){
Row row = sheet.getRow(rowNum);
if(row != null){
// Get the starting column of the current row
int firstCellNum = row.getFirstCellNum();
// Get the number of columns in the current row
int lastCellNum = row.getPhysicalNumberOfCells();
String result = "";
// loop current row
for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++){
Cell cell = row.getCell(cellNum);
double value = 0;
String valueString = cell.getStringCellValue();
if(null!=fe.evaluate(cell)){
value = fe.evaluate(cell).getNumberValue();
}
//result = result + cellNum + ":"+value + "----";
result = result + cellNum + ":"+valueString + "----";
}
System.out.println(result + " ");
}
}
is.close();
} catch (FileNotFoundException e) {
throw new Exception ( "File not correct!");
}
return xlsxList;
}
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("d:/poi.xlsx");
PoiReadExcel re PoiReadExcel = new ();
re.readExcels(is);
}
}
二, JXL
JXL Excel can only operate belong to the older framework, it only supports the version of Excel 95-2000. Now we have stopped updating and maintenance.
pom:
net.sourceforge.jexcelapi
jxl
2.6.10
Export:
public class JxlCreateExcel {
public static void main(String[] args) {
// First, set the table head first row of the table name is the column name
String [] title = {"id","name","sex"};
// Create an Excel file into the path
File file = new File("d:/jxl.xls");
try {
file.createNewFile();
// Create the workbook
WritableWorkbook workbook = Workbook.createWorkbook(file);
// Create sheet
WritableSheet sheet = workbook.createSheet("sheet1",0);
// adding data
Label label = null;
// The first line sets the column name
for (int i = 0; i < title.length; i++) {
label = new Label(i,0,title[i]);
sheet.addCell(label);
}
// append data from the second row from the beginning i
for (int i = 1; i < 9; i++) {
label = new Label(0,i,"id:"+i);
sheet.addCell(label);
label = new Label(1,i,"user");
sheet.addCell(label);
label = new Label(2,i,"男");
sheet.addCell(label);
}
// write and finally close the stream
workbook.write();
workbook.close();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
Import:
public class JxlReadExcel {
public static void main(String[] args) {
try {
// Create Workbook
Workbook workbook = Workbook.getWorkbook(new File("d:/jxl.xls"));
// Get the worksheet sheet
Sheet sheet = workbook.getSheet(0);
// retrieve data
for (int i = 0; i < sheet.getRows(); i++) {// 获取行
for (int j = 0; j <sheet.getColumns (); j ++) {// Get the column
Cell cell = sheet.getCell(j,i);
System.out.print (cell.getContents () + ""); // get the contents of the cell
}
System.out.println();
}
workbook.close();
} catch (Exception e) {
e.printStackTrace ();
}
}
}