博主公众号: 没有腹肌的程序猿
公众号会不定期更新一些数据集,有需要的可以点点关注哦
如何用Java对Excel表进行读写操作?
1.Java读取Excel表的内容
Java读取Excel表相对来说还是比较简单的,分为3步
- 首先是先读取文件
- 再读取Excel表的sheet
- 最后就是通过循环拿到数据
//使用到的包
import jxl.Sheet;
import jxl.Workbook;
String path = "";//Excel文件的路径
Workbook wrb = Workbook.getWorkbook(new File(path));
Sheet rs = wrb.getSheet(0);
//得到一共有多少列
int cols = rs.getColumns();
//得到一共有多少行
int rows = rs.getRows();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
//第一个参数是列 第二个参数是行 然后根据excel对应的每行每列进行取值 第一次遍历的时候name1 第一行第一列 name2 第一行第二列
String name1=rs.getCell(j++, i).getContents();//默认最左边编号也算一列 所以这里得j++
String name2=rs.getCell(j++, i).getContents();
}
}
2.通过Java将数据写入Excel表中
写入数据步骤就相对多一点了.不过我每一步都有注释,应该挺好理解的.不懂可以私信.
- 导入相应的包
- 首先先判断该路径的Excel表是否存在,不存在则新建一个文件
- 定义sheet名和表头信息
- 整理要写入Excel表中的数据
这是相对应的jar包.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
//首先先判断该路径的Excel表是否存在,不存在则新建一个文件
String filePath = "F:\\bank\\"+bank.getName()+".xlsx";
File file = new File(filePath);
if (!file.exists()){
file.createNewFile();
}
//定义sheet名和表头信息 这里表头信息用数组进行存储,sheet表名用List存储
String title[] = {"姓名","性别","年龄"};
List<String> sheetList = new ArrayList<>();
sheetList.add("没有腹肌的程序猿");
//整理要写入Excel表中的数据 一般一行就是一个对象,因此我们要写入Excel表的对象字段先存到一个数组中,再把多个对象信息存储到List中.
List<String[]> arrayList = new ArrayList<>();
//这里假设的数据 userList 使用过程中记得换成自己的数据集
for (int i = 0; i < userList.size(); i++) {
//这里定义数组大小根据自己需要存多少字段进行决定
String userArr[] = new String[3];
user = userList.get(i);
userArr[0] = user.getName();
userArr[1] = user.getSex();
userArr[2] = user.getAge();
//将数组存储arrayList中
arrayList.add(userArr);
}
createExcel(filePath,sheetList,title,arrayList);
public void createExcel(String filePath,List<String> sheetNames,String titleRow[],List<String[]> arrayList){
XSSFWorkbook xWorkbook = new XSSFWorkbook();
//新建文件
FileOutputStream fileOutputStream = null;
XSSFRow row = null;
//HSSFRow row = null;
try {
XSSFCellStyle cellStyle = xWorkbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.LEFT);
cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
//添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
for(int i = 0; i<sheetNames.size(); i++){
XSSFSheet sheet = xWorkbook.createSheet(sheetNames.get(i));
//添加表头
row = xWorkbook.getSheet(sheetNames.get(i)).createRow(0);
//这是设置单元格的大小
row.setHeight((short)(20*20));
//插入表头
for (short j = 0; j < titleRow.length; j++) {
XSSFCell cell = row.createCell(j);
cell.setCellValue(titleRow[j]);
cell.setCellStyle(cellStyle);
}
//插入内容
for (int k = 10; k<arrayList.size();k++) {
XSSFRow contentRow = null;
contentRow = xWorkbook.getSheet(sheetNames.get(i)).createRow(k+1);
String[] array = arrayList.get(k);
for (int j = 0; j < array.length; j++) {
XSSFCell cell = contentRow.createCell(j);
cell.setCellValue(array[j]);
cell.setCellStyle(cellStyle);
}
}
fileOutputStream = new FileOutputStream(filePath);
xWorkbook.write(fileOutputStream);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}