java利用POI實現Excel匯入匯出詳解-支援97-2013版本以及2017版本
阿新 • • 發佈:2019-01-01
package com.kero99.ygc.excel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow ;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
//Excel 匯出 HSSF -->支援97-2013版本 預設版本
//需要的jar包
/**
* commons-io-2.2.jar
* dom4j-1.6.1.jar
* jxl.jar
* poi-3.11-20141221.jar
* poi-examples-3.11-20141221.jar
* poi-excelant-3.11-20141221.jar
* poi-ooxml-3.11-20141221.jar
* poi-ooxml-schemas-3.11-20141221.jar
* poi-scratchpad-3.11-20141221.jar
* xmlbeans-2.6.0.jar
*/
public class PoiExpExcel {
public static void main(String[] args) {
//表頭標題
String[] title={"id","name","sex"};
//建立Excel工作簿
HSSFWorkbook workbook=new HSSFWorkbook();
//建立一個工作表sheet 預設是表名是sheet0
HSSFSheet sheet=workbook.createSheet("ygc開發表");
//設定預設行高
sheet.setDefaultRowHeight ((short)(3 * 256));
//建立字型設定字型為宋體
HSSFFont font = workbook.createFont();
font.setFontName("宋體");
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設定字型高度
font.setFontHeightInPoints((short) 20);
HSSFCellStyle style = workbook.createCellStyle();
//style.setFont(font);
//設定自動換行
style.setWrapText(true);
//設定對齊方式為居中對齊
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
//設定單元格內容垂直對其方式為居中
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
//建立表的第一行
HSSFRow row=sheet.createRow(0);
//建立單元格
HSSFCell cell=null;
//迴圈為第一行三個單元格插入三個標題
for(int i=0;i<title.length;i++){
cell=row.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(title[i]);
}
//追加資料 1是第二行
for(int i=1;i<=10;i++){
HSSFRow nextrow=sheet.createRow(i);
HSSFCell cell2=nextrow.createCell(0);
cell2.setCellStyle(style);
cell2.setCellValue("a"+i);
cell2=nextrow.createCell(1);
cell2.setCellValue("user"+i);
cell2.setCellStyle(style);
cell2=nextrow.createCell(2);
cell2.setCellValue("男");
cell2.setCellStyle(style);
}
//建立一個檔案
File file=new File("e:/測試檔案.xls");
try {
file.createNewFile();
//將Excel存檔
FileOutputStream stream=new FileOutputStream(file);
workbook.write(stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Excel匯入HSSF
package com.kero99.ygc.excel;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiReadExcel {
public static void main(String[] args) {
File file = new File("e:/測試檔案.xls");
//建立Excel,讀取檔案內容
try {
HSSFWorkbook workbook =
new HSSFWorkbook(FileUtils.openInputStream(file));
//建立Excel,讀取檔案內容
// HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(new File("e:/測試檔案.xls")));
//獲取第一個工作表workbook.getSheet("ygc開發表");
// HSSFSheet sheet=workbook.getSheet("ygc開發表");
//第二種獲取方式通過index 獲取第一個表
HSSFSheet sheet = workbook.getSheetAt(0);
int firstRowNum=0;
//獲取表中最後一行
int lastRowNum = sheet.getLastRowNum();
for(int i=firstRowNum;i<=lastRowNum;i++){
HSSFRow row = sheet.getRow(i);
//獲取當前最後單元格列號
int lastCellNum = row.getLastCellNum();
for(int j=0;j<lastCellNum;j++){
HSSFCell cell = row.getCell(j);
String value = cell.getStringCellValue();
System.out.print(value + " ");//注意value後面的空格
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//XSSF 匯入 支援 2017版本
package com.kero99.ygc.excel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class JxlExpExcel {
/**
* XSSF相容高版本Excel檔案 eg:2017版本
* 預設一般建立 HSSF 1997-2013版本
* @param args
*/
public static void main(String[] args) {
//表頭標題
String[] title={"id","name","sex"};
//建立Excel工作簿
XSSFWorkbook workbook=new XSSFWorkbook();
//建立一個工作表sheet 預設是表名是sheet0
Sheet sheet=workbook.createSheet("ygc開發表");
//建立表的第一行
Row row=sheet.createRow(0);
//建立單元格
Cell cell=null;
//迴圈為第一行三個單元格插入三個標題
for(int i=0;i<title.length;i++){
cell=row.createCell(i);
cell.setCellValue(title[i]);
}
//追加資料 1是第二行
for(int i=1;i<=10;i++){
Row nextrow=sheet.createRow(i);
Cell cell2=nextrow.createCell(0);
cell2.setCellValue("a"+i);
cell2=nextrow.createCell(1);
cell2.setCellValue("user"+i);
cell2=nextrow.createCell(2);
cell2.setCellValue("男");
}
//建立一個檔案
File file=new File("e:/測試檔案.xlsx");
try {
file.createNewFile();
//將Excel存檔
FileOutputStream stream=new FileOutputStream(file);
workbook.write(stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/XFFS匯入
package com.kero99.ygc.excel;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class JxlReadExcel {
public static void main(String[] args) {
File file = new File("e:/測試檔案.xlsx");
//建立Excel,讀取檔案內容
try {
XSSFWorkbook workbook =
new XSSFWorkbook(FileUtils.openInputStream(file));
//建立Excel,讀取檔案內容
// Workbook workbook=new Workbook(new FileInputStream(new File("e:/測試檔案.xls")));
//獲取第一個工作表workbook.getSheet("ygc開發表");
// Sheet sheet=workbook.getSheet("ygc開發表");
//第二種獲取方式通過index 獲取第一個表
Sheet sheet = workbook.getSheetAt(0);
int firstRowNum=0;
//獲取表中最後一行
int lastRowNum = sheet.getLastRowNum();
for(int i=firstRowNum;i<=lastRowNum;i++){
Row row = sheet.getRow(i);
//獲取當前最後單元格列號
int lastCellNum = row.getLastCellNum();
for(int j=0;j<lastCellNum;j++){
Cell cell = row.getCell(j);
String value = cell.getStringCellValue();
System.out.print(value + " ");//注意value後面的空格
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
web
//利用下載匯出 可以根據業務需求進行改進
//頭部
HSSFWorkbook wb = new HSSFWorkbook();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
String fileName = "測試檔案.xls";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
//--------------------------HSSF表格製作
//匯出
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();