项目中需要用定时任务去处理 Excel 文件;以前使用的 .csv 文件,现在新需求需要改成 .xls 文件,所以既是完成任务,也是学习 Java 如何处理 Excel 文件。
.xls 文件
直接上完整、可运行的代码:
- 删除指定行,且没有空行
- 删除多余的列
public class DealExcelWithPoi {
/**
* 下面的代码处理逻辑(因为是对模板文件进行处理,所以列数是写死的)
* 对某一列的 “异常” 过滤(即删除指定行,但是不留空白)
*
* @param fileResult 输入需要处理的文件
* @param fileDeal 输出处理后的文件
* @return
* @throws Exception
*/
public String doDealResultData(File fileResult, File fileDeal) throws Exception {
try {
//读入
FileInputStream fis = new FileInputStream(fileResult);//创建输入流
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fis);//解析文件
HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem);//读入excel文件
HSSFSheet sh = workbook.getSheetAt(0);//读取第一个sheet页
// String dealType = sh.getRow(2).getCell(5).getStringCellValue(); //取得第三行第六列的值
// System.out.println(dealType);//异常
int i = sh.getLastRowNum();
String tempRow;
while (i > 1) {//前面两行是列表头,不用操作
tempRow = sh.getRow(i).getCell(10).getStringCellValue();
if ("异常".equals(tempRow)) {
sh.shiftRows(i + 1, sh.getLastRowNum() + 1, -1);
//当前行的后一行到末尾都向前移动一行(相当于覆盖要去掉的行)
// endRow +1操作,是为了大于startRow
}
i--;//放到后面;不然最后一行无法处理到
}
for (int m = 0; m < sh.getLastRowNum() + 1; m++) { //取的行数,是从 0 开始计数
int n = sh.getRow(m).getPhysicalNumberOfCells();
// for (int n = 5; n < 9; n++) { //总共列数
// sh.getRow(m).removeCell(sh.getRow(m).getCell(n));
// }// 不能使用 for 循环的方式(因为你操作的时候,里面的行数或者列数在发生变化)
while (n > 10) { //模板文件里面有10列,需要保留
n--;
sh.getRow(m).removeCell(sh.getRow(m).getCell(n));
}
}
/** 可以去掉匹配的行,但存在空行
int rowNums = sh.getLastRowNum();
System.out.println(rowNums);
for (int row = 2; row < rowNums + 1; row++) {//第三行开始
if ("异常".equals(sh.getRow(row).getCell(5).getStringCellValue())) {
sh.removeRow(sh.getRow(row)); //写出的有空白行
}
}
*/
//读出
FileOutputStream fileOutputStream = new FileOutputStream(fileDeal);
workbook.write(fileOutputStream);
fis.close();
fileOutputStream.close();
} catch (Exception e) {
return "1";
}
return "0";
}
.csv 文件
完整可运行的代码:
public static void main(String[] args) throws IOException {
String inString = "";
int line = 0;
int index = 7;
File file = new File("C:\\Users\\17323\\Desktop\\result.csv"); // CSV文件
File outFile = new File("C:\\Users\\17323\\Desktop\\deal.csv"); // CSV文件
// BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));//设置对应的格式
//将成功数据写入deal文件
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"GBK"));
// BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
while ((inString = reader.readLine()) != null) {
String[] data = inString.split(",");
String result = "";
if (line == 0) {
if ("导入状态".equals(inString)) {
for (int i = 0; i < data.length; i++) {
if ("导入状态".equals(data[i])) {
index = i;
}
}
for (int i = 0; i < data.length - 2; i++) {
if (i == 0) {
result = data[i];
} else {
result += "," + data[i];
}
}
writer.write(result);
writer.newLine();
line++;
continue;
} else {
// 这里默认的模板是 7 列
result = "测试1,测试2,测试3,测试4,测试5,测试6,测试7";
writer.write(result);
writer.newLine();
line++;
continue;
}
}
if ("成功".equals(data[index])) {
for (int i = 0; i < index; i++) {
if (i == 0) {
result = data[i];
} else {
result += "," + data[i];
}
}
writer.write(result);
writer.newLine();
line++;
}
}
writer.close();
reader.close();
}
总结
.xls | .csv |
---|---|
借助poi框架进行解析操作 | 直接可分割(每一列使用 , 即可分割) |
需要注意常见的:
- 文件乱码文件
- 本地和服务器编码问题