|
?1.常用場(chǎng)景 (1)將用戶數(shù)據(jù)導(dǎo)出為excel表格(導(dǎo)出數(shù)據(jù)) (2)將Excel表中的數(shù)據(jù)導(dǎo)出到網(wǎng)站數(shù)據(jù)庫(kù)中 操作Excel最常用的就是Apache的POI和阿里巴巴的easyExcel。 2.官網(wǎng)地址 Apache POI的官網(wǎng):https://poi./ easyExcel的官網(wǎng):https://github.com/alibaba/easyexcel 3.poi和easyExecel的區(qū)別: 以下是我在官網(wǎng)截的圖:
? ? ? ?4.Excel中的對(duì)象 ? ? 在java中萬(wàn)物皆對(duì)象,Excel也不例外,Excel中的對(duì)象有: ? ? 工作簿:Workbook(是一個(gè)接口) ? ? (使用時(shí)需要導(dǎo)入Workbook:import org.apache.poi.ss.usermodel.Workbook;) ? ?Workbook的三個(gè)實(shí)現(xiàn)類(lèi): (1)HSSFWorkbook:HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,擴(kuò)展名是.xls; 工作表:Sheet (使用時(shí)需要導(dǎo)入Sheet:import org.apache.poi.ss.usermodel.Sheet;) ? ? 行:Row? ? ?(使用時(shí)需要導(dǎo)入Row:import org.apache.poi.ss.usermodel.Row;) ? ? 列:Cell??(使用時(shí)需要導(dǎo)入Cell??:import org.apache.poi.ss.usermodel.Cell;) ?5.03版Excel表的生成代碼: package com.lqz.controller;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.ss.usermodel.Workbook;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author:liqinzhen
* @Date:2020/6/9
* @Description:
*/
public class ExcelWriteTest {
String path = "E:\\java\\project\\springmvc-ajax-json";
@Test
public void test() throws IOException {
//1.創(chuàng)建一個(gè)工作簿
Workbook workbook = new HSSFWorkbook();
//2.創(chuàng)建一個(gè)工作表,因?yàn)楣ぷ鞅碓诠ぷ鞑局?,所以用工作簿?chuàng)建工作表
Sheet sheet = workbook.createSheet("學(xué)生表");
//3.創(chuàng)建一個(gè) 行
Row row1 = sheet.createRow(0);
//4.創(chuàng)建一個(gè)單元格
Cell cell11 = row1.createCell(0);
//給單元格設(shè)置值
cell11.setCellValue("陳咬金");;
//創(chuàng)建第二個(gè)單元格
Cell cell12 = row1.createCell(1);
cell12.setCellValue("1021");
//創(chuàng)建第二行
Row row2 = sheet.createRow(1);
//創(chuàng)建第二行的第一個(gè)單元格
Cell cell21 = row2.createCell(0);
cell21.setCellValue("羋月");
//創(chuàng)建第二行的第二個(gè)單元格
Cell cell22 = row2.createCell(1);
cell22.setCellValue("1023");
//生成一張表(io流),03版的Excel使用.xls結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(path "學(xué)生表03.xls");
//輸出
workbook.write(fileOutputStream);
//關(guān)閉流
fileOutputStream.close();
System.out.println("學(xué)生表03.xls表生成完畢");
}
}
6.07版的Excel生成代碼: //(1)導(dǎo)入少量數(shù)據(jù) //(2)批量數(shù)據(jù)導(dǎo)入
@Test
public void test02() throws IOException {
String path = "E:\\java\\project\\springmvc-ajax-json";
//1.創(chuàng)建一個(gè)工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//2.創(chuàng)建一個(gè)工作表
HSSFSheet sheet = workbook.createSheet();
for (int rowNum = 0;rowNum<65536;rowNum ){
//3.創(chuàng)建行
Row row = sheet.createRow(rowNum);
for (int cellNum = 0;cellNum <5;cellNum ){
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("寫(xiě)入完畢");
FileOutputStream fileOutputStream = new FileOutputStream(path "工作表03.xls");
workbook.write(fileOutputStream);
//關(guān)閉流
fileOutputStream.close();
}
//(3)讀取Excel文件 fileInputStream.close(); } //讀取不同數(shù)據(jù)類(lèi)型 ?
? 來(lái)源:https://www./content-4-708351.html |
|
|
來(lái)自: 印度阿三17 > 《開(kāi)發(fā)》