/*
* ==================================================================== 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.
*
* 2012 - Alfresco Software, Ltd. Alfresco Software has modified source of this file The details of changes as svn diff
* can be found in svn at location root/projects/3rd-party/src
* ====================================================================
*/
package com.alibaba.easyexcel.test.temp.dataformat;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.ss.format.CellFormat;
import org.apache.poi.ss.format.CellFormatResult;
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.ExcelGeneralNumberFormat;
import org.apache.poi.ss.usermodel.ExcelNumberFormat;
import org.apache.poi.ss.usermodel.ExcelStyleDateFormatter;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.FractionFormat;
import org.apache.poi.ss.util.DateFormatConverter;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/**
* DataFormatter contains methods for formatting the value stored in an Cell. This can be useful for reports and GUI
* presentations when you need to display data exactly as it appears in Excel. Supported formats include currency, SSN,
* percentages, decimals, dates, phone numbers, zip codes, etc.
* <p>
* Internally, formats will be implemented using subclasses of {@link Format} such as {@link DecimalFormat} and
* {@link java.text.SimpleDateFormat}. Therefore the formats used by this class must obey the same pattern rules as
* these Format subclasses. This means that only legal number pattern characters ("0", "#", ".", "," etc.) may appear in
* number formats. Other characters can be inserted <em>before</em> or <em> after</em> the number pattern to form a
* prefix or suffix.
* </p>
* <p>
* For example the Excel pattern <code>"$#,##0.00 "USD"_);($#,##0.00 "USD")"
* </code> will be correctly formatted as "$1,000.00 USD" or "($1,000.00 USD)". However the pattern
* <code>"00-00-00"</code> is incorrectly formatted by DecimalFormat as "000000--". For Excel formats that are not
* compatible with DecimalFormat, you can provide your own custom {@link Format} implementation via
* <code>DataFormatter.addFormat(String,Format)</code>. The following custom formats are already provided by this class:
* </p>
*
* <pre>
* <ul><li>SSN "000-00-0000"</li>
* <li>Phone Number "(###) ###-####"</li>
* <li>Zip plus 4 "00000-0000"</li>
* </ul>
* </pre>
* <p>
* If the Excel format pattern cannot be parsed successfully, then a default format will be used. The default number
* format will mimic the Excel General format: "#" for whole numbers and "#.##########" for decimal numbers. You can
* override the default format pattern with <code>
* DataFormatter.setDefaultNumberFormat(Format)</code>. <b>Note:</b> the default format will only be used when a Format
* cannot be created from the cell's data format string.
*
* <p>
* Note that by default formatted numeric values are trimmed. Excel formats can contain spacers and padding and the
* default behavior is to strip them off.
* </p>
* <p>
* Example:
* </p>
* <p>
* Consider a numeric cell with a value <code>12.343</code> and format <code>"##.##_ "</code>. The trailing underscore
* and space ("_ ") in the format adds a space to the end and Excel formats this cell as <code>"12.34 "</code>, but
* <code>DataFormatter</code> trims the formatted value and returns <code>"12.34"</code>.
* </p>
* You can enable spaces by passing the <code>emulateCSV=true</code> flag in the <code>DateFormatter</code> cosntructor.
* If set to true, then the output tries to conform to what you get when you take an xls or xlsx in Excel and Save As
* CSV file:
* <ul>
* <li>returned values are not trimmed</li>
* <li>Invalid dates are formatted as 255 pound signs ("#")</li>
* <li>simulate Excel's handling of a format string of all # when the value is 0. Excel will output "",
* <code>DataFormatter</code> will output "0".
* </ul>
* <p>
* Some formats are automatically "localized" by Excel, eg show as mm/dd/yyyy when loaded in Excel in some Locales but
* as dd/mm/yyyy in others. These are always returned in the "default" (US) format, as stored in the file. Some format
* strings request an alternate locale, eg <code>[$-809]d/m/yy h:mm AM/PM</code> which explicitly requests UK locale.
* These locale directives are (currently) ignored. You can use {@link DateFormatConverter} to do some of this
* localisation if you need it.
*/
public class DataFormatter1 implements Observer {
private static final String defaultFractionWholePartFormat = "#";
private static final String defaultFractionFractionPartFormat = "#/##";
/**
* Pattern to find a number format: "0" or "#"
*/
private static final Pattern numPattern = Pattern.compile("[0#]+");
/**
* Pattern to find days of week as text "ddd...."
*/
private static final Pattern daysAsText = Pattern.compile("([d]{3,})", Pattern.CASE_INSENSITIVE);
/**
* Pattern to find "AM/PM" marker
*/
private static final Pattern amPmPattern = Pattern.compile("((A|P)[M/P]*)", Pattern.CASE_INSENSITIVE);
/**
* Pattern to find formats with condition ranges e.g. [>=100]
*/
private static final Pattern rangeConditionalPattern =
Pattern.compile(".*\\[\\s*(>|>=|<|<=|=)\\s*[0-9]*\\.*[0-9].*");
/**
* A regex to find locale patterns like [$$-1009] and [$?-452]. Note that we don't currently process these into
* locales
*/
private static final Pattern localePatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+\\])");
/**
* A regex to match the colour formattings rules. Allowed colours are: Black, Blue, Cyan, Green, Magenta, Red,
* White, Yellow, "Color n" (1<=n<=56)
*/
private static final Pattern colorPattern = Pattern.compile("(\\[BLACK\\])|(\\[BLUE\\])|(\\[CYAN\\])|(\\[GREEN\\])|"
+ "(\\[MAGENTA\\])|(\\[RED\\])|(\\[WHITE\\])|(\\[YELLOW\\])|"
+ "(\\[COLOR\\s*\\d\\])|(\\[COLOR\\s*[0-5]\\d\\])", Pattern.CASE_INSENSITIVE);
/**
* A regex to identify a fraction pattern. This requires that replaceAll("\\?", "#") has already been called
*/
private static final Pattern fractionPattern = Pattern.compile("(?:([#\\d]+)\\s+)?(#+)\\s*\\/\\
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
JAVA解析Excel工具;Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便
资源推荐
资源详情
资源评论
收起资源包目录
Chat2DB 智能数据库客户端,数据报表工具,自然语言生成SQL,生成报表 (620个子文件)
mvnw.cmd 7KB
lombok.config 73B
converterCsv.csv 1KB
office_bom.csv 126B
no_bom.csv 123B
simple.csv 113B
.editorconfig 309B
.gitignore 188B
maven-wrapper.jar 57KB
DataFormatter1.java 50KB
WriteTest.java 37KB
DataFormatter.java 33KB
ExcelWriteFillExecutor.java 27KB
ClassUtils.java 25KB
WriteContextImpl.java 21KB
AbstractWriteHolder.java 20KB
FillStyleDataTest.java 19KB
Lock2Test.java 18KB
FillStyleAnnotatedTest.java 18KB
CsvSheet.java 18KB
DateUtils.java 17KB
AbstractExcelWriteExecutor.java 16KB
ReadTest.java 13KB
PoiTest.java 13KB
WriteCellStyle.java 13KB
WriteHandler.java 13KB
StyleDataTest.java 13KB
XlsxSaxAnalyser.java 12KB
WriteWorkbookHolder.java 12KB
StyleUtil.java 11KB
FillTest.java 10KB
EasyExcelFactory.java 10KB
FillTempTest.java 10KB
ExcelWriteAddExecutor.java 10KB
ExcludeOrIncludeDataTest.java 10KB
BuiltinFormats.java 9KB
StyleProperty.java 9KB
FillDataTest.java 9KB
ExcelAnalyserImpl.java 9KB
DefaultConverterLoader.java 9KB
TempLargeDataTest.java 9KB
StringUtils.java 9KB
CacheDataTest.java 9KB
CsvWorkbook.java 8KB
WriteHandlerUtils.java 8KB
StyleTest.java 8KB
FileUtils.java 8KB
XlsSaxAnalyser.java 8KB
CsvCell.java 8KB
WriteV33Test.java 8KB
ExcelReaderBuilder.java 7KB
Validate.java 7KB
ModelBuildEventListener.java 7KB
ConverterUtils.java 7KB
FieldUtils.java 7KB
ParameterDataTest.java 7KB
DataFormatTest.java 7KB
DefaultAnalysisEventProcessor.java 7KB
LargeDataTest.java 6KB
TempWriteTest.java 6KB
CompatibilityTest.java 6KB
ListUtils.java 6KB
SharedStringsTableHandler.java 6KB
ExcelWriteHeadProperty.java 6KB
NumberUtils.java 6KB
ReadWorkbookHolder.java 6KB
Ehcache.java 6KB
WriteTest.java 6KB
CsvCellStyle.java 6KB
CsvExcelReadExecutor.java 5KB
ReadCellData.java 5KB
Write.java 5KB
WorkBookUtil.java 5KB
XlsxRowHandler.java 5KB
MapUtils.java 5KB
AnalysisContextImpl.java 5KB
NoModelDataTest.java 5KB
FillAnnotationDataTest.java 5KB
CellWriteHandler.java 5KB
AnnotationDataTest.java 5KB
ExcelHeadProperty.java 5KB
FillTempTest.java 5KB
CellTagHandler.java 5KB
ContentStyle.java 5KB
BomDataTest.java 5KB
HeadStyle.java 5KB
ReadAllConverterDataListener.java 5KB
ConverterDataTest.java 4KB
AbstractReadHolder.java 4KB
ExcelWriterBuilder.java 4KB
PoiWriteTest.java 4KB
WebTest.java 4KB
ExcelWriter.java 4KB
FontProperty.java 4KB
SortDataTest.java 4KB
FormulaRecordHandler.java 4KB
ExcelTypeEnum.java 4KB
ClientAnchorData.java 4KB
CsvReadTest.java 4KB
WriteCellData.java 4KB
共 620 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6651
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功