POI操作Excel
POIpackage com.zy.test; import org.apache.poi.hssf.usermodel.HSSFCell; 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.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class OutTest { //alt+回车 //03版 @Test public void test1() throws Exception {//导出 //面向对象,找对象解决问题 //1创建工作簿 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); //2创建工作表 HSSFSheet sheet = hssfWorkbook.createSheet("工作表1"); //3创建行【下标0开始】 HSSFRow row = sheet.createRow(3); //4创建单元格 参数是列【下标0开始】 HSSFCell cell = row.createCell(3); cell.setCellValue("爱我中华"); //输出流 FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\a\\Desktop\\abc.xls"); hssfWorkbook.write(fileOutputStream); fileOutputStream.close(); } @Test public void test2() throws Exception { FileInputStream fileInputStream = new FileInputStream("C:\\Users\\a\\Desktop\\abc.xls"); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileInputStream); HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0); HSSFRow row = sheetAt.getRow(3); HSSFCell cell = row.getCell(3); String stringCellValue = cell.getStringCellValue(); System.out.println(stringCellValue); } //07以后版本 @Test public void test3() throws Exception { XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); XSSFSheet sheet = xssfWorkbook.createSheet("工作表1"); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell(0); cell.setCellValue("我是小妖怪"); FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\a\\Desktop\\ABC.xlsx"); xssfWorkbook.write(fileOutputStream); fileOutputStream.close(); } @Test public void test4() throws Exception { FileInputStream fileInputStream = new FileInputStream("C:\\Users\\a\\Desktop\\ABC.xlsx"); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream); XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0); XSSFRow row = sheetAt.getRow(0); XSSFCell cell = row.getCell(0); String stringCellValue = cell.getStringCellValue(); System.out.println(stringCellValue); } //读取03或07版本的excel @Test public void read03And07() throws Exception { // String filePath="C:\\Users\\a\\Desktop\\abc.xls"; String filePath="C:\\Users\\a\\Desktop\\ABC.xlsx"; if (filePath.matches("^.+\\.(?i)((xls)|(xlsx))$")){ FileInputStream fileInputStream = new FileInputStream(filePath); boolean is03Excel = filePath.matches("^.+\\.(?i)(xls)$")?true:false; Workbook workbook= is03Excel? new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream); Sheet sheetAt = workbook.getSheetAt(0); Row row = sheetAt.getRow(0); Cell cell = row.getCell(0); String stringCellValue = cell.getStringCellValue(); System.out.println(stringCellValue); } } }
所需jar包