java excel 导入导出 list套list 外层list是行数 里边是列数

/**
	 * 导入Excel
	 * @param execelFile excel文件的绝对路径
	 * @param index	从第几行开始读取
	 * @param impOrExpExcel 导入进库的接口
	 * @throws IOException
	 * @throws FileNotFoundException
	 */

	public static List<List<String>> impExcel(String execelFile,int index) throws FileNotFoundException, IOException {

		List<List<String>> alllist = new ArrayList<List<String>>();

		// 构造 Workbook 对象,execelFile 是传入文件路径(获得Excel工作区)
		FileInputStream fileInputStream = new FileInputStream(execelFile);
		Workbook book = null;
		try {
			// Excel 2007获取方法
			book = new XSSFWorkbook(fileInputStream);
		} catch (Exception ex) {
			try {
				// Excel 2003获取方法
				book = new HSSFWorkbook(fileInputStream);
			}
			catch (Exception e){
				throw e;
			}
			finally {
				fileInputStream.close();
			}
		}

		// 读取表格的第一个sheet页
		Sheet sheet = book.getSheetAt(0);
		// 定义 row、cell
		Row row;
		String cell;
		// 总共有多少行,从0开始
		int totalRows = sheet.getLastRowNum();

		// 循环输出表格中的内容,首先循环取出行,再根据行循环取出列
		for (int i = index; i <= totalRows; i++) {
			row = sheet.getRow(i);
			// 处理空行
			if (row == null) {
				continue;
			}
			//每一行的值的集合
			List<String> arrList = new ArrayList<String>();

			// 总共有多少列,从0开始
			int totalCells = row.getLastCellNum();
			for (int j = row.getFirstCellNum(); j < totalCells; j++) {
				// 处理空列
				if (row.getCell(j) == null) {
					arrList.add("");
					continue;
				}
				// 通过 row.getCell(j).toString() 获取单元格内容
				cell = row.getCell(j).toString();
				arrList.add(cell);
			}

			alllist.add(arrList);
		}

		return alllist;

	}
/**
 * 导出Excel表
 * @param expFilePath 导出的路径
 * @param sheetName Sheet名称
 * @param list 数据
 * @throws IOException
 */
public static String expExcel(String expFilePath,String sheetName,List<List<String>> list) throws IOException {

   OutputStream os = null;
   Workbook book = null;
   String suffix = null;
   try {
      // 创建工作区(97-2003,2007)
      try {
         // Excel 2007获取方法
         book = new XSSFWorkbook();
         suffix = ".xlsx";

      } catch (Exception ex) {
         // Excel 2003获取方法
         book = new HSSFWorkbook();
         suffix = ".xls";
      }
      expFilePath = expFilePath + suffix;

      //创建相应的文件
      File file = new File(expFilePath);
      if (!file.isDirectory())
         file.createNewFile();

      // 输出流
      os = new FileOutputStream(expFilePath);

      // 创建第一个sheet页
      Sheet sheet = book.createSheet(sheetName);
      int count = list.size();
      if (list == null || count == 0)
         return null;

      for (int i = 0; i < count; i++) {
         List<String> subList = list.get(i);
         int columnCount = subList.size();//列数
         Row row = sheet.createRow(i);// 生成新行
         for (int j = 0; j < columnCount; j++) {
            // 给每一行每一列赋值
            row.createCell(j).setCellValue(subList.get(j));
         }
      }
      // 写文件
      book.write(os);

      return suffix;
   } catch (FileNotFoundException e) {
      throw e;
   } catch (IOException e) {
      throw e;
   } finally {
      // 关闭输出流
      if (os != null) {
         try {
            os.close();
         } catch (IOException e) {
               throw e
         }
      }
   }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

softwareDragon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值