日期:2014-05-16 浏览次数:21144 次
最近和csv文件杠上了,今天将xlsx和xls文件转成csv文件,然后再对转换后的csv文件执行一些操作,但是我调用Apache的官方例子的时候,转换是成功了,执行操作的时候一直报错,文件被占用的错误。
主要的转换代码我都下载了,调用其实很简单:
XLSX2CSV xlsx2csv = new XLSX2CSV(filePath, toFile); xlsx2csv.process();
?我想肯定是process方法里面没有释放掉内存,但是这个类是Apache的官方例子,应该不会有错的啊。
结果我到XLSX2CSV这个类里面找,果然没有close方法,于是乎,我就写了个close方法:
public void close(){ this.output.close(); }
?
然后把这个close方法加到process方法的结尾就OK了。
public void process() throws IOException, OpenXML4JException, ParserConfigurationException, SAXException { ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable( this.xlsxPackage); XSSFReader xssfReader = new XSSFReader(this.xlsxPackage); StylesTable styles = xssfReader.getStylesTable(); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader .getSheetsData(); int index = 0; while (iter.hasNext()) { InputStream stream = iter.next(); String sheetName = iter.getSheetName(); this.output.println(); this.output.println(sheetName + " [index=" + index + "]:"); processSheet(styles, strings, stream); stream.close(); ++index; } close(); }
?
这样就解决了csv文件在程序结束前一直被占用的问题。?
下面和大家共享下apache的官方代码,还是不错的。
XLSX转CSV:
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package com.cnas.fileConversion; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; /** * A rudimentary XLSX -> CSV processor modeled on the POI sample program * XLS2CSVmra by Nick Burch from the package * org.apache.poi.hssf.eventusermodel.examples. Unlike the HSSF version, this * one completely ignores missing rows. * <p/> * Data sheets are read using a SAX parser to keep the memory footprint * relatively small, so this should be able to read enormous wo