Excel的上传下载

来源:互联网 发布:c 能给plc编程吗 编辑:程序博客网 时间:2024/06/03 23:04

今天我们来看Excel文件的上传下载,这里我们使用到了Apache POI,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。

poi的结构

HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。

HSSF与XSSF的区别

HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx

例子

首先要导入jar包,这里我们使用了maven来管理

<dependency>    <groupId>org.apache.poi</groupId>    <artifactId>poi-ooxml</artifactId>    <version>3.8</version></dependency>

java代码

package com.cloiot;  import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.poi.ss.usermodel.Cell;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 com.cloiot.framework.util.DateUtils;public class ExcelUtils {      public static void main(String[] args) {          try {              getExcelAsFile("d:/测试-201608171732.xlsx");          } catch (FileNotFoundException e) {              e.printStackTrace();          } catch (IOException e) {              e.printStackTrace();          }        String[] header = new String[]{"编号", "数值"};        List<List<String>> list = new ArrayList<>();        List<String> subList = new ArrayList<String>();        subList.add("1");        subList.add("20");        list.add(subList);        String fileExportName = "测试";        ExcelUtils.exportExcel(header, list, fileExportName);    }      /**      * 得到Excel,并解析内容      * @param file      * @throws FileNotFoundException      * @throws IOException      */      public static void getExcelAsFile(String file) throws FileNotFoundException, IOException{          XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));  // 得到Excel工作簿对象        XSSFSheet sheet = wb.getSheetAt(0);  // 获取第一个sheet        int trLength = sheet.getLastRowNum();  //总行数          for(int i=0; i<=trLength; i++){              XSSFRow row = sheet.getRow(i);  // 得到Excel工作表的行              int tdLength = row.getLastCellNum();  //总列数              for(int j=0; j<tdLength; j++){                  XSSFCell cell = row.getCell(j);  //得到Excel工作表指定行的单元格                  /**                  * 为了处理:Excel异常Cannot get a text value from a numeric cell                  * 将所有列中的内容都设置成String类型格式                  */                  if(cell!=null){                        cell.setCellType(Cell.CELL_TYPE_STRING);                  }                  //获得每一列中的值                  System.out.print(cell.getStringCellValue()+"\t\t\t");              }              System.out.println();          }      }    /**     * 下载 excel数据表     * @param response     * @param header:excel表头     * @param list:数据列表     * @param fileExportName:下载文件名     */    public static <E> void exportExcel(String[] header, List<List<String>> list, String fileExportName) {        XSSFWorkbook wb = new XSSFWorkbook();//创建工作簿        XSSFSheet sheet = wb.createSheet();//创建一个sheet        XSSFRow headerRow = sheet.createRow(0);//创建一行        XSSFRow contentRow = null;        //设置标题        for(int i=0;i<header.length;i++){            headerRow.createCell(i).setCellValue(header[i]);        }        try {            for(int i=0; i<list.size(); i++){                contentRow = sheet.createRow(i+1);                List<String> rowData = list.get(i);                for (int j=0; j<rowData.size();j++) {                    contentRow.createCell(j).setCellValue(rowData.get(j));                }            }        } catch (IllegalArgumentException e) {            e.printStackTrace();        } catch (SecurityException e) {            e.printStackTrace();        }        OutputStream os = null;        SimpleDateFormat formatter = new SimpleDateFormat(DateUtils.YYYYMMDD_12);        String dateStr = "-" + formatter.format(new Date());        String name = fileExportName+dateStr+".xlsx";        try {            os = new FileOutputStream(new File("D:/"+name));            wb.write(os);            os.flush();        } catch (Exception e) {            e.printStackTrace();        } finally{            if(os!=null){                try {                    os.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}  
0 0
原创粉丝点击