网站的ftp怎么查,中国视觉设计网,网页优化怎么做,好的响应式网站有哪些简介#xff1a;Apache POI 是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是#xff0c;我们可以使用POI在Java程序中对Miscrosoft Office各种文件进行读写操作。 目录 1、应用场景
2、案例代码
2.1 创建 Excel 文件
2.2 读取 Excel 文件 1、应用场景
… 简介Apache POI 是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是我们可以使用POI在Java程序中对Miscrosoft Office各种文件进行读写操作。 目录 1、应用场景
2、案例代码
2.1 创建 Excel 文件
2.2 读取 Excel 文件 1、应用场景
图 1-1 Apache POI应用场景 Apache POI应用场景 银行网银系统导出交易明细各种业务系统导出Excel报表批量导入业务数据
2、案例代码
2.1 创建 Excel 文件
package com.sky.test;import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class POITest {public static void main(String[] args) throws IOException {write();}/*** 通过POI创建Excel文件并且写入文件内容*/public static void write() throws IOException {//创建一个Excel文件XSSFWorkbook excel new XSSFWorkbook();//创建Excel中的单元测XSSFSheet sheet excel.createSheet(info);//在sheet中创建行对象,rownum编号从零开始XSSFRow row sheet.createRow(1);//创建单元格并且写入文件内容row.createCell(1).setCellValue(姓名);row.createCell(2).setCellValue(城市);//创建一个新行XSSFRow row1 sheet.createRow(2);row1.createCell(1).setCellValue(王宁);row1.createCell(2).setCellValue(亳州);//创建一个新行XSSFRow row2 sheet.createRow(2);row2.createCell(1).setCellValue(王宁);row2.createCell(2).setCellValue(亳州);//将内存中的Excel表格存储到指定的盘符下面FileOutputStream fileOutputStream new FileOutputStream(new File(D:\\info.xlsx));excel.write(fileOutputStream);//关闭资源excel.close();fileOutputStream.close();}
}2.2 读取 Excel 文件
package com.sky.test;import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.*;public class POITest {public static void main(String[] args) throws IOException {read();}/*** 通过POI读取Excel文件*/public static void read() throws IOException{FileInputStream fileInputStream new FileInputStream(new File(D:\\info.xlsx));//创建一个Excel对象用于读取Excle数据XSSFWorkbook excel new XSSFWorkbook(fileInputStream);//读取Excel文件中的第一个Sheet页XSSFSheet sheet excel.getSheetAt(0);//获取Sheet页中有数据的最后一行int lastRowNum sheet.getLastRowNum();for (int i 1; i lastRowNum; i) {//获取某一行XSSFRow row sheet.getRow(i);//获取当前行的单元格对象String cellValue1 row.getCell(1).getStringCellValue();String cellValue2 row.getCell(2).getStringCellValue();System.out.println(cellValue1 cellValue2);}//关闭资源excel.close();fileInputStream.close();}
}