private File file; //上传的文件 private String fileFileName; //文件名称 private String fileContentType; //文件类型 /** * 导出数据 * @return */ public String exportData(){ formBean=this.provinceInfoServiceImpl.searchAll(formBean); List<TProvinceinfo> exprotData = formBean.getResultList(); session.put("exprotData", exprotData); return SUCCESS; } /** * 导入数据 * @return * @throws Exception */ public String importData() throws Exception{ //创建文件输入流对象 FileInputStream is = new FileInputStream(file) ; //通过输入流创建工作簿 HSSFWorkbook workBook = new HSSFWorkbook(is); HSSFSheet sheet = workBook.getSheetAt(0); //获取总行数 int totalRowNum = sheet.getLastRowNum(); //这里i<== 循环遍历导入工作簿行数 取出相关数据 for (int i = 0; i <= totalRowNum; i++) { HSSFRow row = sheet.getRow(i); for (int j = 0; j < row.getLastCellNum(); j++) { //打印出导入的内容 System.out.print(row.getCell(j)); } System.out.println(""); } dataMap.put("uploadResult", true); return SUCCESS; }
package com.lmy.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.hy.entity.TProvinceinfo; public class Download extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=exportData.xls"); ServletOutputStream outStream = response.getOutputStream(); //创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("省数据"); HttpSession session = request.getSession(); //从session中获取相关数据 List<TProvinceinfo>exprotData=(List<TProvinceinfo>) session.getAttribute("exprotData"); //初始化标题 List<String> title = new ArrayList<String>(3); title.add("省级ID"); title.add("省级名称"); title.add("备注"); //创建行 HSSFRow row = sheet.createRow(0); //创建单元格 HSSFCell cell = row.createCell(0);// //赋值 cell.setCellValue(title.get(0)); HSSFCell cell1 = row.createCell(1);// cell1.setCellValue(title.get(0)); HSSFCell cell2 = row.createCell(2);// cell2.setCellValue(title.get(2)); for (int i = 1; i <= exprotData.size(); i++) { //循环数据 创建对应行数据 HSSFRow rows = sheet.createRow(i);//创建行 HSSFCell cells1 = rows.createCell(0);// cells1.setCellValue(exprotData.get(i-1).getFProvinceID()); HSSFCell cells2 = rows.createCell(1);// cells2.setCellValue(exprotData.get(i-1).getFProviceName()); HSSFCell cells3 = rows.createCell(2);// cells3.setCellValue(exprotData.get(i-1).getFRemark()); } //导出工作簿 workbook.write(outStream); outStream.flush(); outStream.close(); } }