zoukankan      html  css  js  c++  java
  • 使用POI完成 EXCEL的 导出和导入

        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();
            
        }
    
    }
  • 相关阅读:
    设计一个栈,除了pop与push方法,还支持Min方法,可返回栈元素中的最小值,push、pop、min三个方法的时间复杂度必须是O(1)
    删除单向链表中间的某个节点,假定你只能访问该节点
    找出单向链表中倒数第K个节点
    关于ssh纠错笔记
    3维图像分割显示 标签: 图像分割level set3dmatlab 2015-04-16 17:59 228人阅读 评论(0)
    EM 算法 标签: clusteringalgorithm图像分割EM算法 2015-03-24 21:26 426人阅读 评论(0)
    kmeans算法分割图像 标签: kmeans图像分割算法 2015-03-23 20:02 509人阅读 评论(0)
    随机数
    IDL 日期时间函数整理
    显示程序运行时间_matlab
  • 原文地址:https://www.cnblogs.com/cnjava/p/3076245.html
Copyright © 2011-2022 走看看