一、常用java包
Apache POI包中的HSSFWorkbook、XSSFWorkbook、SXSSFWorkbook区别如下:
(1) HSSFWorkbook:一般用于操作Excel2003以前的版本,扩展名是.xls。
(2) XSSFWorkbook:一般用于操作Excel2007及以上的版本,扩展名是.xlsx。
(3) SXSSFWorkbook:一般用于大数据量的导出。比如数据量超过5000条即可考虑这种工作表。
二、maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>2.1.0</version>
</dependency>
三、java代码处理
注意:读取文件报错,可将excel另存为xlsx格式再试,手动修改格式可能无效!!!
import com.monitorjbl.xlsx.StreamingReader;
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.*;
import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ReadAndWriteExcel {
public static void main(String[] args) throws IOException {
//生成证件的excel
CreatePidExcel();
//读取证件excel
//具体操作:读取路径(E:\Pid-Hash8.xlsx)下的excel,并将第二例加1,赋值到第三列
ReadAndWriteExcel();
}
//生成证件的excel
public static void CreatePidExcel() throws IOException {
//创建WorkBook对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建sheet页签
HSSFSheet sheet = wb.createSheet("Pid-Hash-Sheet");
//设置列宽
sheet.setColumnWidth(0,5000);
sheet.setColumnWidth(1,5000);
//首行赋值
HSSFRow row0 = sheet.createRow( 0 );
row0.createCell(0 ).setCellValue("pid值");
row0.createCell(1 ).setCellValue("对应的hash值");
//创建行
for(int i=1;i<100;i++){
BigDecimal b = new BigDecimal("123");
String c = b.add(BigDecimal.valueOf(i)).toString();
HSSFRow row = sheet.createRow( i );
//赋值每行的第一例
row.createCell(0).setCellValue(c);
//赋值每行的第二列
row.createCell(1).setCellValue(c.hashCode());
}
//输出Excel文件
FileOutputStream output= new FileOutputStream( "d:\\Pid-Hash9.xlsx" );
wb.write(output);
output.flush();
}
//读取证件excel,并在第二列的值加1,赋值到第三列
public static void ReadAndWriteExcel() throws IOException {
FileInputStream in = new FileInputStream(new File("E:\\Pid-Hash8.xlsx"));
Map<String, List<String>> mapData = new HashMap<String, List<String>>();
Workbook wk = StreamingReader.builder()
.rowCacheSize(100) //缓存到内存中的行数,默认是10
.bufferSize(4096) //读取资源时,缓存到内存的字节大小,默认是1024
.open(in);
//创建HSSFWorkbook对象
HSSFWorkbook wbnew = new HSSFWorkbook();
//创建Sheet页
HSSFSheet newsheet = wbnew.createSheet( "sheet1" );
//设置列宽
newsheet.setColumnWidth(0,5000);
newsheet.setColumnWidth(1,5000);
//获取所有的sheet页数量
int sheetNums = wk.getNumberOfSheets();
//处理每个sheet页
for(int i = 0 ; i < sheetNums;i ++){
Sheet sheet = wk.getSheetAt(i);
String sheetName = wk.getSheetName(i);
//遍历所有的行
int k = 0;
for (Row row : sheet) {
//遍历剩余的行
if(0 == row.getRowNum()){
HSSFRow row1 = newsheet.createRow( row.getRowNum() );
//首行,前两列赋值
for (Cell cell : row) {
row1.createCell(cell.getColumnIndex()).setCellValue(cell.getStringCellValue());
}
//首行,第三列赋值
row1.createCell(2).setCellValue("更新后的hash值");
}else{
HSSFRow row2 = newsheet.createRow( row.getRowNum() );
for (Cell cell : row) {
//第2行开始,前两列赋值
row2.createCell(cell.getColumnIndex()).setCellValue(cell.getStringCellValue());
//第2行开始,第三列hash值加1
if(1 == cell.getColumnIndex()){
//获取第二列的hash值
BigDecimal b = new BigDecimal(cell.getStringCellValue());
//将hash值加1
String c = b.add(BigDecimal.valueOf(1)).toString();
//赋值到第三列
row2.createCell(2).setCellValue(c);
}
}
}
}
}
//输出Excel文件
FileOutputStream output= new FileOutputStream( "E:\\Pid-Hash81.xlsx" );
wbnew.write(output);
output.flush();
}
}
四、处理结果
1、处理前的文件
2、处理后的文件