论坛首页 Java企业应用论坛

jxl读取excel需要关闭Workbook?

浏览 3231 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-23  
	/**
	 * 简单的读取excel
	 * @param inputFile
	 * @param inputFileSheetIndex
	 * @throws Exception
	 */
	public static ArrayList<String> sampleReadExcel(File inputFile, int inputFileSheetIndex) throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		Workbook book = null;
		Cell cell = null;
		//避免乱码的设置
		WorkbookSettings setting = new WorkbookSettings(); 
		java.util.Locale locale = new java.util.Locale("zh","CN"); 
		setting.setLocale(locale);
		setting.setEncoding("ISO-8859-1");
		book = Workbook.getWorkbook(inputFile, setting);

		Sheet sheet = book.getSheet(inputFileSheetIndex);
		for (int rowIndex = 0; rowIndex < sheet.getRows(); rowIndex++) {// Excel第一行为表头,因此J初值设为1
			for (int colIndex = 0; colIndex < sheet.getColumns(); colIndex++) {// 只需从Excel中取出2列
				cell = sheet.getCell(colIndex, rowIndex);
				list.add(cell.getContents());
			}
		}

		//【问题:如果在实际部署的时候没有写下面这句是否会导致不断消耗掉服务器的内存?jxl里面有个ReadWrite.java没有关闭读的,只关闭了写的】
		book.close();
		
		return list;
	}
   发表时间:2008-04-23  
它会把所有数据都读到内存中,如果数据量太大的时候,就肯定暴了
看看你调用它的代码。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics