解決POI讀取Excel記憶體溢出的問題__java基礎學習

來源:互聯網
上載者:User

Office軟體一直是一個誨譽參半的軟體,廣大普通電腦使用者用Office來滿足日常辦公需求,於是就產生了很多生產資料和文檔,需要和企業單位的專用辦公系統對接,而Office的解析工作一直是程式員非常頭痛的問題,經常招致程式員的謾罵,也被譽為是微軟最爛的發明之一。POI的誕生解決了Excel的解析難題(POI即“討厭的試算表”,確實很討厭,我也很討厭Excel),但如果用不好POI,也會導致程式出現一些BUG,例如記憶體溢出,假空行,公式等等問題。下面介紹一種解決POI讀取Excel記憶體溢出的問題。

        POI讀取Excel有兩種模式,一種是使用者模式,一種是SAX模式,將xlsx格式的文檔轉換成CVS格式後再進行處理使用者模式相信大家都很清楚,也是POI常用的方式,使用者模式API介面豐富,我們可以很容易的使用POI的API讀取Excel,但使用者模式消耗的記憶體很大,當遇到很多sheet、大資料格、假空行、公式等問題時,很容易導致記憶體溢出。POI官方推薦解決記憶體溢出的方式使用CVS格式解析,我們不可能手工將Excel檔案轉換成CVS格式再上傳,這樣做太麻煩了,好再POI給出了xlsx轉換CVS的例子,基於這個例子我進行了一下改造,即可解決使用者模式讀取Excel記憶體溢出的問題。下面附上代碼:

     [java]   view plain  copy /* ====================================================================     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.  ==================================================================== */      import java.io.IOException;   import java.io.InputStream;   import java.io.PrintStream;   import java.text.SimpleDateFormat;   import java.util.ArrayList;   import java.util.Date;   import java.util.List;      import javax.xml.parsers.ParserConfigurationException;   import javax.xml.parsers.SAXParser;   import javax.xml.parsers.SAXParserFactory;      import org.apache.poi.hssf.usermodel.HSSFDateUtil;   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.InputSource;  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.