java 中的poi_Java中使用POI操作ExceL的读与
1.Java中使用POI操作ExceL的讀與寫
?直接給代碼
1.1導入依賴
org.apache.poi
poi
3.10-FINAL
org.apache.poi
poi-ooxml
3.10-FINAL
1.2 使用工具類
package cn.Poi;
import cn.domain.Customer;
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.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 java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtils {
/**
* 、創建單元格
* @param excelDir 路徑
* @param excelFileName 文件名
* @throws Exception
*/
public static void createExcel2007(File excelDir , String excelFileName) throws Exception{
// 1. 內存中創建一個excel工作薄
Workbook wb = new HSSFWorkbook(); //創建了一個2007之前格式的excel文件
// 創建頁
createSheet(wb,10);
// 創建行和列
fillData(wb.getSheet("第0頁"),10,10);
// 2. 保存這個excel 磁盤上
File excelFile = new File(excelDir,excelFileName);
final FileOutputStream out = new FileOutputStream(excelFile);
wb.write(out);
out.close();
}
/**
* 同上
* @param excelDir
* @param excelFileName
* @throws Exception
*/
public static void createExcel2008(File excelDir , String excelFileName) throws Exception{
// 1. 內存中創建一個excel工作薄
Workbook wb = new XSSFWorkbook();//創建了一個2007之前格式的excel文件
// 2. 保存這個excel 磁盤上
File excelFile = new File(excelDir,excelFileName);
final FileOutputStream out = new FileOutputStream(excelFile);
wb.write(out);
out.close();
}
/**
* 創建文檔頁碼
* @param wb
* @param sheetNum
*/
public static void createSheet(Workbook wb , int sheetNum){
for (int i = 0; i < sheetNum; i++) {
wb.createSheet("第"+i+"頁");
}
}
/**
* 創建單元格,并且在里面填充數字進行測試,寫入測試
* @param sheet
* @param row
* @param col
*/
public static void fillData(Sheet sheet , int row , int col){
//創建行
for (int i = 0; i < row; i++) {
Row currentRow = sheet.createRow(i);
//每行多少個單元格
for (int i1 = 0; i1 < col; i1++) {
//獲取單元格,并且設置單元格里面的值
currentRow.createCell(i1).setCellValue(i*col+i1);
}
}
}
/**、
* 測試讀取數字,從第0頁開始
* @throws Exception
*/
public static void readExcelint() throws Exception{
FileInputStream excelFile = new FileInputStream("C:/Users\\10596\\Desktop\\firstExcel.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(excelFile);
final HSSFSheet sheet = hssfWorkbook.getSheet("第0頁");
//分別獲取 文檔中最后一行的行標,單元格
for (int i = 0 ; i
final HSSFRow row = sheet.getRow(i);
for (int j = 0 ; j
double value = row.getCell(j).getNumericCellValue();
System.out.println(value);
}
}
}
/**、
* 測試讀取文字,從第0頁開始
* @throws Exception
*/
public static void readExcelStr() throws Exception{
FileInputStream excelFile = new FileInputStream("C:/Users\\10596\\Desktop\\customerList.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(excelFile);
final HSSFSheet sheet = hssfWorkbook.getSheet("第0頁");
//分別獲取 文檔中最后一行的行標,單元格
for (int i = 0 ; i
final HSSFRow row = sheet.getRow(i);
for (int j = 0 ; j
String stringCellValue = row.getCell(j).getStringCellValue();
System.out.println(stringCellValue);
}
}
}
/**
* 寫入數據
* @throws Exception
*/
public static void db2excel() throws Exception{
List list = new ArrayList();
for(int i = 0; i < 100; i++){
Customer customer = new Customer();
customer.setId(i+"jack");
customer.setName("jack"+i);
customer.setAddress("測試"+i);
list.add(customer);
}
// 1. 內存中創建一個excel工作薄
Workbook wb = new HSSFWorkbook(); //創建了一個2007之前格式的excel文件
// 創建頁
createSheet(wb,10);
// 創建行和列
Field[] declaredFields = Customer.class.getDeclaredFields();
//在第0頁填充數據
Sheet sheet = wb.getSheet("第0頁");
//獲取集合的大小,間接性獲取有多少個對象需要存入到文檔
for (int i = 0; i < list.size() ; i++){
Customer customer = list.get(i);
//有多少對象就有多少行,創建單元行
Row row = sheet.createRow(i);
int col = 0;
for (Field field : declaredFields) {
//反射的 實體類有 私有屬性,進行暴力反射
field.setAccessible(true);
//獲取對象里面的值,填充到單元格中
String value = (String) field.get(customer);
row.createCell(col++).setCellValue(value);
}
}
// 2. 保存這個excel 磁盤上
File excelFile = new File("C:\\\\Users\\\\10596\\\\Desktop","customerList.xls");
final FileOutputStream out = new FileOutputStream(excelFile);
wb.write(out);
out.close();
}
public static void main(String[] args) throws Exception {
// createExcel2007(new File("C:\\Users\\10596\\Desktop"),"firstExcel.xls");
// readExcelint(); //讀取填充的int數
//填充數據
db2excel();
//讀取填充的字符串
readExcelStr();
}
}
1.3 使用寫入時創建的實體類
package cn.domain;
public class Customer {
private String id;
private String name;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
1.4 包結構,以及效果
包結構
寫入的效果
讀取文檔打印的效果
標簽:Java,String,poi,ExceL,new,POI,import,public,wb
來源: https://blog.csdn.net/Violet_201903027/article/details/100177857
總結
以上是生活随笔為你收集整理的java 中的poi_Java中使用POI操作ExceL的读与的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度云盘archlinux manjar
- 下一篇: 计算机职业倾向自我评价50字,自我评价5