JAVA实现EXCEL表格文件(.xls格式)的读取、修改、保存、另存为、排序等操作。大佬们帮帮忙

JAVA实现EXCEL表格文件(.xls格式)的读取、修改、保存、另存为、排序等操作。大佬们帮帮忙文件格式参考下图

import Java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

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;
public class ReadExcel {
public static void readExcel(File file){
try {
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
Workbook wb = null;
// poi-3.9.jar 只可以读取2007以下的版本,后缀为:xsl
wb = new HSSFWorkbook(inputStream);//解析xls格式

Sheet sheet = wb.getSheetAt(0);//第一个工作表 ,第二个则为1,以此类推...

int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
Row row = sheet.getRow(rIndex);
if(row != null){
int firstCellIndex = row.getFirstCellNum();
// int lastCellIndex = row.getLastCellNum();
//此处参数cIndex决定可以取到excel的列数。
for(int cIndex = firstCellIndex; cIndex < 3; cIndex ++){
Cell cell = row.getCell(cIndex);
String value = "";
if(cell != null){
value = cell.toString();
System.out.print(value+"\t");
}
}
System.out.println();
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
File file = new File("D:/test.xls");
readExcel(file);
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答