Excelが頻繁に日常業務で使用し、春ブーツは、POI Excelを操作するに使用されます
1つの新しい春のブートMavenのサンプルプロジェクト
注:この例では、IDEAの開発ツールであります
- ファイル>新規>プロジェクト、以下の図を選択
Spring Initializr
して、[次へ]をクリックします次 - フィル
GroupId
(パッケージ名)、Artifact
(プロジェクト名)にすることができます。[次へ]をクリックします
のgroupId = com.fishpro
たartifactId =エクセル - 選択は異なり
Spring Web Starter
、フロントダニを。 - プロジェクト名が設定されています
spring-boot-study-excel
。
ファイルのアップロードは、サードパーティのコンポーネントの導入を必要としません。
2依存性導入のpom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3操作エクセル
本実施例の動作のExcelの異なるバージョンを有する異なるクラスはサフィックスXLSバージョンを使用します。詳細については、公式ドキュメントを
3.1ワークブックの作成
HSSFWorkbook
拡張機能のバージョン(2003年を含む)前の操作Excel2003は.XLSです。XSSFWorkbook
オペレーティングExcel2007のバージョンでは、拡張子が.xlsxのです。SXSSFWorkbook
オペレーティングExcel2007のバージョンでは、拡張子が.xlsxのです。
public static void CreateNewWorkbook() {
Workbook wb = new HSSFWorkbook();
try {
OutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
Workbook wb2 = new XSSFWorkbook();
try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
wb2.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
}
3.2ワークシートを作成します
- シート名が31文字を超えてはなりません
- 名前に特殊文字を含めることはできません
- WorkbookUtil.createSafeSheetNameは、安全なワークシート名を作成するために使用することができます
public static void CreateNewSheet() {
Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("new second sheet");
String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns " O'Brien's sales "
Sheet sheet3 = wb.createSheet(safeName);
try {
OutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
}
3.3セルのセルを作成します
- まず、最初の行のセルの作成で作成した列線は、そこにあります
- スタイルの作成
- date型の値を作成します。
- 作成日、小数、文字、boolean型など
- 境界型セルを作成します。
- 単位セルの書式設定データ
/**
*## 3.3 创建单元格 Cells
* - 先有行在有列,先要创建 Row 在创建 Cell
* */
public void CreateNewCell() {
Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
//先行后列
Row row = sheet1.createRow(0);
//创建列
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
//创建一个列的样式
CellStyle cellStyle = wb.createCellStyle();
//获取一个帮助类设置样式
CreationHelper createHelper = wb.getCreationHelper();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//使用 Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
//创建不同的类型的单元格
row.createCell(3).setCellValue(1.1);
row.createCell(4).setCellValue(new Date());
row.createCell(5).setCellValue(Calendar.getInstance());
row.createCell(6).setCellValue("a string");
row.createCell(7).setCellValue(true);
row.createCell(8).setCellType(CellType.ERROR);
try {
OutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
}
エクセル3.4を読むとget
ファイルは、Excel使用方法を読んで
、 `` `Javaの
パブリック静的な無効OpenExcelByFile(){
試み{
ワークブックWorkbookFactory.create WB =(新しい新しいファイル( "workbook.xlsを"));
//読み
シートシート= wb.getSheetAt(0 ); //最初の
シートのシート1 = wb.getSheet( "シート1" ); // の名前読み取り
行の行を= sheet.getRow(0); // 行取得
セルセルを= row.getCell(0); / /最初の行を取得します
}catch (Exception ex){
}
}
`` `
使用FileInputStreamのは、メモリのサポートを必要とします
public static void OpenExcelByFileInputStream(){
try {
Workbook wb = WorkbookFactory.create(new FileInputStream("workbook.xls"));
//遍历
}catch (Exception ex){
}
}
参考: