首页 > 其他分享 >Excel文件导入导出

Excel文件导入导出

时间:2022-10-11 18:12:44浏览次数:44  
标签:int Excel excel 导出 xssfSheet 导入 poi new xssfCell

依赖包为apach的poi包

  	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi</artifactId>
	    <version>3.16</version>
	</dependency>
  	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi-ooxml</artifactId>
	    <version>3.16</version>
	</dependency>
文件导入

文件导入比较简单,poi包里面有专门的类处理excel文件,XSSFWorkbook,XSSFSheet,XSSFRow,XSSFCell分别为不同的层级

public class XmlFileImport {
    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\shenp\\Desktop\\部分三方数据调用统计(1).xlsx";
        File file = new File(path);

//        JFileChooser jFileChooser = new JFileChooser();
//        int state=jFileChooser.showOpenDialog(null);
//        File file2 = jFileChooser.getSelectedFile();

        //获取excel
        XSSFWorkbook xs = new XSSFWorkbook(new FileInputStream(file));
        List<String> reslist = new ArrayList<>();
        int sheetcount = xs.getNumberOfSheets();
        for (int i = 0; i < sheetcount; i++) {
            //获取excel的sheet
            XSSFSheet xssfSheet = xs.getSheetAt(i);
            if(xssfSheet==null){
                continue;
            }
            for (int i1 = 0; i1 < xssfSheet.getLastRowNum(); i1++) {
                //获取excel中的行
                XSSFRow xssfRow = xssfSheet.getRow(i1);
                if(xssfRow==null){
                    continue;
                }
                for (int i2 = 0; i2 < xssfRow.getLastCellNum(); i2++) {
                    //获取excel中的单元格
                    XSSFCell xssfCell = xssfRow.getCell(i2);
                    if(xssfCell==null){
                        continue;
                    }
                    if(xssfCell.getCellType()== CellType.STRING){
                        reslist.add(xssfCell.getStringCellValue());
                    }
                }
            }
        }
        log.info(JSONArray.toJSONString(reslist));
    }
}
文件导出

文件导出建议直接使用工具类

参考https://gitee.com/782560705/easyexcel.git

标签:int,Excel,excel,导出,xssfSheet,导入,poi,new,xssfCell
From: https://www.cnblogs.com/yorkiiz/p/16780092.html

相关文章