package com.sniper.util.excel;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.sniper.util.Tools;
public class ExcelUtils {
public static File createEmptyExcelFile(File aOutFile, String...aSheetNames) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
for (int i = 0; i < aSheetNames.length; ++i) {
wb.createSheet( aSheetNames[i] );
// 持有电子表格数据的xml文件名 例如 /xl/worksheets/sheet1.xml
//String sheetName = nextSheet.getPackagePart().getPartName().getName();
//System.err.println( sheetName );
}
File templateFile = new File( aOutFile.getParentFile(), "template" + Generator.next() + ".xlsx" );
// 保存模板
OutputStream os = new BufferedOutputStream( new FileOutputStream( templateFile ) );
try {
wb.write( os );
return templateFile;
}
finally {
try {
os.close();
}
catch (Throwable ignore) {
}
try {
wb.close();
}
catch (Throwable ignore) {
}
}
}
public static void copyStream(InputStream in, OutputStream out) throws IOException {
byte [] buffer = Tools.byteBufferLocal.get();
for (int c = in.read( buffer ); c >= 0; c = in.read( buffer )) {
out.write( buffer, 0, c );
}
}
public static InternalCharSequence getProperty(CharSequence aChars, int aBeginIndex, int aEndIndex, char aPropertyChar, InternalCharSequence aResult) {
for (int i = aBeginIndex; i < aEndIndex; ++i) {
if (aChars.charAt( i ) == aPropertyChar && aChars.charAt( i + 1 ) == '=' && aChars.charAt( i + 2 ) == '\"') {
int b = i + 3;
int e = b;
while (true) {
if (aChars.charAt( e ) == '\"') {
return aResult.reset( aChars, b, e );
}
else {
++e;
}
}
}
}
return aResult.reset( aChars, 0, 0 );
}
public static String encodeXml(String aString) {
if (aString == null) {
return "";
}
StringBuilder buffer = null;
// loop over all the characters of the String.
for (int i = 0, isize = aString.length(); i < isize; i++) {
char character = aString.charAt( i );
if(character == '\'' || character == '\"' || character == '&' || character == '<' || character == '>'){
if(buffer == null){
buffer = new StringBuilder(isize + 20);
buffer.append( aString.substring( 0, i ) );
}
switch(character){
case '\'':
buffer.append( "'" );
break;
case '\"':
buffer.append( """ );
break;
case '&':
buffer.append( "&" );
break;
case '<':
buffer.append( "<" );
break;
case '>':
buffer.append( ">" );
break;
}
}
else if(buffer != null){
buffer.append( character );
}
}
return buffer == null ? aString : buffer.toString();
}
public static InternalCharSequence getTagInternalText(CharSequence aChars, int aBeginIndex, int aEndIndex, char aTagChar, InternalCharSequence aResult) {
//<f>A1*B1</f> ==> A1*B1
//<v>ABC</v> ==> ABC
for (int i = aBeginIndex; i < aEndIndex; ++i) {//<f>A1*B1</f>
if (aChars.charAt( i ) == '<' && aChars.charAt( ++i ) == aTagChar && aChars.charAt( ++i ) == '>') {
int b = ++i;
while (true) {
if (aChars.charAt( i ) == '<') {
return aResult.reset( aChars, b, i );
}
else {
++i;
}
}
}
}
return aResult.reset( aChars, 0, 0 );
}
public static boolean charsEquals(CharSequence aOne, CharSequence aTwo) {
int isize = aOne.length();
if (isize == aTwo.length()) {
for (int i = 0; i < isize; ++i) {
if (aOne.charAt( i ) != aTwo.charAt( i )) {
return false;
}
}
return true;
}
return false;
}
public static boolean isInteger(CharSequence aString) {
return ExcelUtils.isInteger( aString, 10 );
}
public static boolean isInteger(CharSequence aStr, int aRadix) throws NumberFormatException {
if (aStr == null) {
throw new NumberFormatException( "null" );
}
if (aRadix < Character.MIN_RADIX) {
throw new NumberFormatException( "radix " + aRadix + " less than Character.MIN_RADIX" );
}
if (aRadix > Character.MAX_RADIX) {
throw new NumberFormatException( "radix " + aRadix + " greater than Character.MAX_RADIX" );
}
int result = 0;
boolean negative = false;
int i = 0, max = aStr.length();
int limit;
int multmin;
int digit;
if (max > 0) {
if (aStr.charAt( 0 ) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
}
else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / aRadix;
if (i < max) {
digit = Character.digit( aStr.charAt( i++ ), aRadix );
if (digit < 0) {
//throw NumberFormatException.forInputString(s);
return false;
}
else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit( aStr.charAt( i++ ), aRadix );
if (digit < 0) {
//throw NumberFormatException.forInputString( aStr );
return false;
}
if (result < multmin) {
//throw NumberFormatException.forInputString( aStr );
return false;
}
result *= aRadix;
if (result < limit + digit) {
//throw NumberFormatException.forInputString( aStr );
return false;
}
result -= digit;
}
}
else {
//throw NumberFormatException.forInputString( aStr );
return false;
}
if (negative) {
if (i > 1) {
return true;
}
else { /* Only got "-" */
//throw NumberFormatException.forInputString( aStr );
return false;
}
}
else {
//return -result;
return true;
}
}
static int parseInt(CharSequence aString) {
return ExcelUtils.parseInt( aString, 10 );
}
static int parseInt(CharSequence aStr, int aRadix) throws NumberFormatException {
if (aStr == null) {
throw new NumberFormatException( "null" );
}
if (aRadix < Character.MIN_RADIX) {
thro