Java實現對Excel數據的讀和寫

package com.StepStepUp.step;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ExcelTest {
public static void main(String[] args) throws Throwable{
// 創建 Excel 文件的輸入流對象
FileInputStream excelFileInputStream = new FileInputStream("E:/TestDemo.xlsx");
// XSSFWorkbook 就代表一個 Excel 文件
// 創建其對象,就打開這個 Excel 文件
XSSFWorkbook workbook = new XSSFWorkbook(new BufferedInputStream(excelFileInputStream));

// 輸入流使用後,及時關閉!這是文件流操作中極好的一個習慣!
excelFileInputStream.close();
// XSSFSheet 代表 Excel 文件中的一張表格
// 我們通過 getSheetAt(0) 指定表格索引來獲取對應表格
// 注意表格索引從 0 開始!
XSSFSheet sheet = workbook.getSheetAt(0);




// 開始循環表格數據,表格的行索引從 0 開始
// employees.xlsx 第一行是標題行,我們從第二行開始, 對應的行索引是 1
// sheet.getLastRowNum() : 獲取當前表格中最後一行數據對應的行索引
for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
// XSSFRow 代表一行數據
XSSFRow row = sheet.getRow(rowIndex);
if (row == null) {
continue;
}
XSSFCell nameCell = row.getCell(0); // 姓名列
XSSFCell genderCell = row.getCell(1); // 性別列
XSSFCell ageCell = row.getCell(2); // 年齡列
XSSFCell weightCell = row.getCell(3); // 體重列
XSSFCell salaryCell = row.getCell(4); // 收入列
StringBuilder employeeInfoBuilder = new StringBuilder();
employeeInfoBuilder.append("員工信息 --> ")
.append("姓名 : ").append(nameCell.getStringCellValue())
.append(" , 性別 : ").append(genderCell.getStringCellValue())
.append(" , 年齡 : ").append(ageCell.getNumericCellValue())
.append(" , 體重(千克) : ").append(weightCell.getNumericCellValue())
.append(" , 月收入(元) : ").append(salaryCell.getNumericCellValue());
System.out.println(employeeInfoBuilder.toString());
}
// 操作完畢後,記得要將打開的 XSSFWorkbook 關閉
//workbook.close();      //這行代碼加上以後會有問題,會直接把當前的Excel中的數據給清空

// ------ 創建一行新的數據 ----------//
// 指定行索引,創建一行數據, 行索引爲當前最後一行的行索引 + 1
int currentLastRowIndex = sheet.getLastRowNum();
int newRowIndex = currentLastRowIndex + 1;
XSSFRow newRow = sheet.createRow(newRowIndex);
// 開始創建並設置該行每一單元格的信息,該行單元格的索引從 0 開始
int cellIndex = 0;
// 創建一個單元格,設置其內的數據格式爲字符串,並填充內容,其餘單元格類同
XSSFCell newNameCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
newNameCell.setCellValue("錢七");
XSSFCell newGenderCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
newGenderCell.setCellValue("女");
XSSFCell newAgeCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
newAgeCell.setCellValue(50);
XSSFCell newWeightCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
newWeightCell.setCellValue(68);
XSSFCell newSalaryCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
newSalaryCell.setCellValue(6000);

// 將最新的 Excel 數據寫回到原始 Excel 文件(就是D盤那個 Excel 文件)中       
// 首先要創建一個原始Excel文件的輸出流對象!
FileOutputStream excelFileOutPutStream = new FileOutputStream("E:/TestDemo.xlsx");
// 將最新的 Excel 文件寫入到文件輸出流中,更新文件信息!
workbook.write(excelFileOutPutStream);
// 執行 flush 操作, 將緩存區內的信息更新到文件上
excelFileOutPutStream.flush();
// 使用後,及時關閉這個輸出流對象, 好習慣,再強調一遍!
excelFileOutPutStream.close();
}

}



PS:需要引入POI的jar包











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章