基本信息
源码名称:java 读写excel 示例代码
源码大小:7.30KB
文件格式:.java
开发语言:Java
更新时间:2015-04-25
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.nnu.yd.pink.util; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.nnu.yd.pink.constant.PinkConstant; import com.nnu.yd.pink.entity.ActivityReport; import com.nnu.yd.pink.entity.DownLoad; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import java.util.List; import java.util.ArrayList; public class ExcelUtil { public static List<DownLoad> readExcel2007_Down(InputStream in, int gid, int gradeid) { InputStream fis = in; List<DownLoad> sheetData = new ArrayList<DownLoad>(); try { XSSFWorkbook xwb = new XSSFWorkbook(in); // 读取第一章表格内容 XSSFSheet sheet = xwb.getSheetAt(0); // 定义 row、cell XSSFRow row; String cell; // 循环输出表格中的内容 for (int i = sheet.getFirstRowNum(); i < sheet .getPhysicalNumberOfRows(); i ) { DownLoad u = new DownLoad(); row = sheet.getRow(i); if (i != 0) { // u.setUsername(row.getCell(0).toString()); // u.setAge(Integer.parseInt(row.getCell(1) // .getRichStringCellValue().getString())); // if (row.getCell(2).toString().equals("男")) { // u.setSex("0"); // } else { // u.setSex("1"); // } // List<WebParent> ls = new ArrayList<WebParent>(); // // // u.setLs(ls); // u.setGid(gid); // u.setGradeid(gradeid); // u.setDegree(row.getCell(4).toString()); // u.setMobile(row.getCell(5).toString()); // u.setResume(row.getCell(6).toString()); // u.setGid(cid); sheetData.add(u); } } } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return sheetData; } // public static List<ActivityReport> readExcel2007(InputStream in, int cid) { // InputStream fis = in; // List<ActivityReport> sheetData = new ArrayList<ActivityReport>(); // // try { // XSSFWorkbook xwb = new XSSFWorkbook(in); // // 读取第一章表格内容 // XSSFSheet sheet = xwb.getSheetAt(0); // // 定义 row、cell // XSSFRow row; // String cell; // // 循环输出表格中的内容 // for (int i = sheet.getFirstRowNum(); i < sheet // .getPhysicalNumberOfRows(); i ) { // // ActivityReport u = new ActivityReport(); // row = sheet.getRow(i); // if (i != 0) { // u.setUsername(row.getCell(0).toString()); // u.setDuty(row.getCell(1).toString()); // if (row.getCell(2).toString().equals("男")) { // u.setSex("0"); // } else { // u.setSex("1"); // } // // u.setAge(Integer.parseInt(row.getCell(3).getRawValue())); // u.setDegree(row.getCell(4).toString()); // u.setMobile(row.getCell(5).toString()); // u.setResume(row.getCell(6).toString()); // u.setGid(cid); // sheetData.add(u); // } // // } // } catch (IOException e) { // e.printStackTrace(); // } finally { // if (fis != null) { // try { // fis.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // } // return sheetData; // } // // public static List<WebTeacher> readExcel2003(InputStream in, int cid) { // // InputStream fis = in; // List<WebTeacher> sheetData = new ArrayList<WebTeacher>(); // // try { // // // 获得一个excel实例 // HSSFWorkbook workbook = new HSSFWorkbook(fis); // // 取得excel文件的第一个工作表 // HSSFSheet sheet = workbook.getSheetAt(0); // // 通过迭代器遍历获得行数据后遍历行数据获得单元格数据,并保存在列表sheetData中 // Iterator rows = sheet.rowIterator(); // int i = 0; // while (rows.hasNext()) { // // if (i != 0) { // HSSFRow row = (HSSFRow) rows.next(); // Iterator cells = row.cellIterator(); // // WebTeacher u = new WebTeacher(); // // HSSFCell cell = (HSSFCell) cells.next(); // u.setUsername(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setCompany(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setDuty(cell.getStringCellValue()); // // // // cell = (HSSFCell) cells.next(); // // u.setRole(cell.getStringCellValue()); // // // // cell = (HSSFCell) cells.next(); // // u.setTitle(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setEmail(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setTrade(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setCity(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setMobile(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setPostcode(cell.getStringCellValue()); // // cell = (HSSFCell) cells.next(); // // u.setProvince(cell.getStringCellValue()); // // // // u.setConferenceid(cid); // // u.setT_url(""); // sheetData.add(u); // } // i ; // } // } catch (IOException e) { // e.printStackTrace(); // } finally { // if (fis != null) { // try { // fis.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // } // return sheetData; // } // public boolean writeExcel2007DownLoad(List<DownLoad> ap, String filename) { // 输出流 boolean res = true; OutputStream os; try { os = new FileOutputStream(PinkConstant.FILEPATH filename ".xlsx"); // 工作区 XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("download"); // 如果循环超过10172次,则报内存溢出,有谁循环超过10万次不报错,麻烦请告诉我,这样是因为可以一次性导出大量数据 for (int i = 0; i < ap.size(); i ) { // 创建第一个sheet // 生成第一行 DownLoad d = ap.get(i); XSSFRow row = sheet.createRow(i); // 给这一行的第一列赋值 if(d.getOs().contains("iPhone OS")){ row.createCell(0).setCellValue("IOS"); }else if(d.getOs().contains("Windows")){ row.createCell(0).setCellValue("WP7"); }else{ row.createCell(0).setCellValue("Android"); } row.createCell(1).setCellValue(d.getOs()); row.createCell(2).setCellValue(d.getTimes()); row.createCell(3).setCellValue(d.getDate()); // 给这一行的第一列赋值 // row.createCell(1).setCellValue(ap.get(i).getCompany()); // row.createCell(2).setCellValue(ap.get(i).getDuty()); // row.createCell(3).setCellValue(ap.get(i).getRole()); } // 写文件 wb.write(os); // 关闭输出流 os.close(); } catch (FileNotFoundException e) { res = false; e.printStackTrace(); } catch (IOException e) { res = false; e.printStackTrace(); } return res; } public static void main(String[] args) { } }