日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 中的poi_Java中使用POI操作ExceL的读与

發(fā)布時間:2024/3/7 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 中的poi_Java中使用POI操作ExceL的读与 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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 {

/**

* 、創(chuàng)建單元格

* @param excelDir 路徑

* @param excelFileName 文件名

* @throws Exception

*/

public static void createExcel2007(File excelDir , String excelFileName) throws Exception{

// 1. 內(nèi)存中創(chuàng)建一個excel工作薄

Workbook wb = new HSSFWorkbook(); //創(chuàng)建了一個2007之前格式的excel文件

// 創(chuàng)建頁

createSheet(wb,10);

// 創(chuàng)建行和列

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. 內(nèi)存中創(chuàng)建一個excel工作薄

Workbook wb = new XSSFWorkbook();//創(chuàng)建了一個2007之前格式的excel文件

// 2. 保存這個excel 磁盤上

File excelFile = new File(excelDir,excelFileName);

final FileOutputStream out = new FileOutputStream(excelFile);

wb.write(out);

out.close();

}

/**

* 創(chuàng)建文檔頁碼

* @param wb

* @param sheetNum

*/

public static void createSheet(Workbook wb , int sheetNum){

for (int i = 0; i < sheetNum; i++) {

wb.createSheet("第"+i+"頁");

}

}

/**

* 創(chuàng)建單元格,并且在里面填充數(shù)字進行測試,寫入測試

* @param sheet

* @param row

* @param col

*/

public static void fillData(Sheet sheet , int row , int col){

//創(chuàng)建行

for (int i = 0; i < row; i++) {

Row currentRow = sheet.createRow(i);

//每行多少個單元格

for (int i1 = 0; i1 < col; i1++) {

//獲取單元格,并且設(shè)置單元格里面的值

currentRow.createCell(i1).setCellValue(i*col+i1);

}

}

}

/**、

* 測試讀取數(shù)字,從第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);

}

}

}

/**

* 寫入數(shù)據(jù)

* @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. 內(nèi)存中創(chuàng)建一個excel工作薄

Workbook wb = new HSSFWorkbook(); //創(chuàng)建了一個2007之前格式的excel文件

// 創(chuàng)建頁

createSheet(wb,10);

// 創(chuàng)建行和列

Field[] declaredFields = Customer.class.getDeclaredFields();

//在第0頁填充數(shù)據(jù)

Sheet sheet = wb.getSheet("第0頁");

//獲取集合的大小,間接性獲取有多少個對象需要存入到文檔

for (int i = 0; i < list.size() ; i++){

Customer customer = list.get(i);

//有多少對象就有多少行,創(chuàng)建單元行

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數(shù)

//填充數(shù)據(jù)

db2excel();

//讀取填充的字符串

readExcelStr();

}

}

1.3 使用寫入時創(chuàng)建的實體類

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 包結(jié)構(gòu),以及效果

包結(jié)構(gòu)

寫入的效果

讀取文檔打印的效果

標簽:Java,String,poi,ExceL,new,POI,import,public,wb

來源: https://blog.csdn.net/Violet_201903027/article/details/100177857

總結(jié)

以上是生活随笔為你收集整理的java 中的poi_Java中使用POI操作ExceL的读与的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。