这期内容当中小编将会给大家带来有关Java的poi技术怎样读取和导入Excel,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
项目结构:
用到的Excel文件:
XlsMain .java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; 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; public class XlsMain { public static void main(String[] args) throws IOException { XlsMain xlsMain = new XlsMain(); XlsDto xls = null; List<XlsDto> list = xlsMain.readXls(); try { XlsDto2Excel.xlsDto2Excel(list); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < list.size(); i++) { xls = (XlsDto) list.get(i); System.out.println(xls.getXh() + " " + xls.getXm() + " " + xls.getYxsmc() + " " + xls.getKcm() + " " + xls.getCj()); } } private List<XlsDto> readXls() throws IOException { InputStream is = new FileInputStream("pldrxkxxmb.xls"); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); XlsDto xlsDto = null; List<XlsDto> list = new ArrayList<XlsDto>(); // 循环工作表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 循环行Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } xlsDto = new XlsDto(); // 循环列Cell // 0学号 1姓名 2学院 3课程名 4 成绩 // for (int cellNum = 0; cellNum <=4; cellNum++) { HSSFCell xh = hssfRow.getCell(0); if (xh == null) { continue; } xlsDto.setXh(getValue(xh)); HSSFCell xm = hssfRow.getCell(1); if (xm == null) { continue; } xlsDto.setXm(getValue(xm)); HSSFCell yxsmc = hssfRow.getCell(2); if (yxsmc == null) { continue; } xlsDto.setYxsmc(getValue(yxsmc)); HSSFCell kcm = hssfRow.getCell(3); if (kcm == null) { continue; } xlsDto.setKcm(getValue(kcm)); HSSFCell cj = hssfRow.getCell(4); if (cj == null) { continue; } xlsDto.setCj(Float.parseFloat(getValue(cj))); list.add(xlsDto); } } return list; } @SuppressWarnings("static-access") private String getValue(HSSFCell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { // 返回布尔类型的值 return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { // 返回数值类型的值 return String.valueOf(hssfCell.getNumericCellValue()); } else { // 返回字符串类型的值 return String.valueOf(hssfCell.getStringCellValue()); } } }
XlsDto2Excel.java类
//该类主要负责向Excel(2003版)中插入数据
import java.io.FileOutputStream; import java.io.OutputStream; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class XlsDto2Excel { public static void xlsDto2Excel(List<XlsDto> xls) throws Exception { // 获取总列数 int CountColumnNum = xls.size(); // 创建Excel文档 HSSFWorkbook hwb = new HSSFWorkbook(); XlsDto xlsDto = null; // sheet 对应一个工作页 HSSFSheet sheet = hwb.createSheet("pldrxkxxmb"); HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始 HSSFCell[] firstcell = new HSSFCell[CountColumnNum]; String[] names = new String[CountColumnNum]; names[0] = "学号"; names[1] = "姓名"; names[2] = "学院"; names[3] = "课程名"; names[4] = "成绩"; for (int j = 0; j < CountColumnNum; j++) { firstcell[j] = firstrow.createCell(j); firstcell[j].setCellValue(new HSSFRichTextString(names[j])); } for (int i = 0; i < xls.size(); i++) { // 创建一行 HSSFRow row = sheet.createRow(i + 1); // 得到要插入的每一条记录 xlsDto = xls.get(i); for (int colu = 0; colu <= 4; colu++) { // 在一行内循环 HSSFCell xh = row.createCell(0); xh.setCellValue(xlsDto.getXh()); HSSFCell xm = row.createCell(1); xm.setCellValue(xlsDto.getXm()); HSSFCell yxsmc = row.createCell(2); yxsmc.setCellValue(xlsDto.getYxsmc()); HSSFCell kcm = row.createCell(3); kcm.setCellValue(xlsDto.getKcm()); HSSFCell cj = row.createCell(4); cj.setCellValue(xlsDto.getCj()); (xlsDto.getMessage()); } } // 创建文件输出流,准备输出电子表格 OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmb.xls"); hwb.write(out); out.close(); System.out.println("数据库导出成功"); } }
XlsDto .java类
//该类是一个实体类
public class XlsDto { private Integer xkh; private String xh; private String xm; private String yxsmc; private Integer kch; private String kcm; private float cj; public Integer getXkh() { return xkh; } public void setXkh(Integer xkh) { this.xkh = xkh; } public String getXh() { return xh; } public void setXh(String xh) { this.xh = xh; } public String getXm() { return xm; } public void setXm(String xm) { this.xm = xm; } public String getYxsmc() { return yxsmc; } public void setYxsmc(String yxsmc) { this.yxsmc = yxsmc; } public Integer getKch() { return kch; } public void setKch(Integer kch) { this.kch = kch; } public String getKcm() { return kcm; } public void setKcm(String kcm) { this.kcm = kcm; } public float getCj() { return cj; } public void setCj(float cj) { this.cj = cj; } }
后台输出:
数据库导出成功
0 hongten 信息技术学院 计算机网络应用基础 80.0
0 王五 信息技术学院 计算机网络应用基础 81.0
0 李胜基 信息技术学院 计算机网络应用基础 82.0
0 五班古 信息技术学院 计算机网络应用基础 83.0
0 蔡诗芸 信息技术学院 计算机网络应用基础 84.0
上述就是小编为大家分享的Java的poi技术怎样读取和导入Excel了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网行业资讯频道。