Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.scudata.excel;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.scudata.common.CellLocation;
import com.scudata.common.Matrix;
import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.common.StringUtils;
import com.scudata.dm.BaseRecord;
import com.scudata.dm.ComputeStack;
import com.scudata.dm.Context;
import com.scudata.dm.Current;
import com.scudata.dm.DataStruct;
import com.scudata.dm.FileObject;
import com.scudata.dm.ILineInput;
import com.scudata.dm.ILineOutput;
import com.scudata.dm.Sequence;
import com.scudata.dm.Table;
import com.scudata.dm.cursor.ICursor;
import com.scudata.expression.Expression;
import com.scudata.resources.AppMessage;
import com.scudata.resources.EngineMessage;
import com.scudata.util.Variant;
/**
* Tools for operating excel
*
*/
public class ExcelTool implements ILineInput, ILineOutput {
/**
* Interface for operating excel
*/
private IExcelTool impl;
/**
* Check the jdk version, it cannot be lower than 1.6.
*/
private void checkVersion() throws RQException {
String v = System.getProperty("java.specification.version");
if (Float.parseFloat(v) < 1.6)
throw new RQException(AppMessage.get().getMessage(
"excel.jreversion"));
}
/**
* Constructor used to export Excel
*
* @param fo
* FileObject
* @param hasTitle
* Is there a header row
* @param isXlsx
* true means xlsx format, false means xls format.
* @param isSsxxf
* Whether to export xlsx files by stream
* @param sheetName
* Excel sheet name
*/
public ExcelTool(FileObject fo, boolean hasTitle, boolean isXlsx,
boolean isSsxxf, Object sheetName) {
this(fo, hasTitle, isXlsx, isSsxxf, sheetName, null);
}
/**
* Constructor used to export Excel
*
* @param fo
* FileObject
* @param hasTitle
* Is there a header row
* @param isXlsx
* true means xlsx format, false means xls format.
* @param isSsxxf
* Whether to export xlsx files by stream
* @param sheetName
* Excel sheet name
* @param pwd
* The password of the excel
*/
public ExcelTool(FileObject fo, boolean hasTitle, boolean isXlsx,
boolean isSsxxf, Object sheetName, String pwd) {
this(fo, hasTitle, isXlsx, isSsxxf, true, sheetName, pwd, false, false);
}
/**
* Constructor used to export Excel
*
* @param fo
* FileObject
* @param hasTitle
* Is there a header row
* @param isXlsx
* true means xlsx format, false means xls format.
* @param isSsxxf
* Whether to export xlsx files by stream
* @param sheetName
* Excel sheet name
* @param pwd
* The password of the excel
* @param isW
* Option @w.
*/
public ExcelTool(FileObject fo, boolean hasTitle, boolean isXlsx,
boolean isSsxxf, boolean isAppend, Object sheetName, String pwd,
boolean isW, boolean isK) {
if (isXlsx) {
checkVersion();
if (isSsxxf) {
impl = new XlsxSExporter(fo, hasTitle, isAppend, sheetName,
pwd, isK);
} else {
impl = new XlsxExporter(fo, hasTitle, isAppend, sheetName, pwd,
isK);
}
} else {
impl = new XlsExporter(fo, hasTitle, isAppend, sheetName, pwd, isK);
}
}
/**
* Constructor used to import Excel
*
* @param fis
* InputStream
* @param isXlsx
* true means xlsx format, false means xls format.
*/
public ExcelTool(InputStream fis, boolean isXlsx) {
this(fis, isXlsx, null);
}
/**
* Constructor used to import Excel
*
* @param fis
* InputStream
* @param isXlsx
* true means xlsx format, false means xls format.
* @param pwd
* The password of the excel
*/
public ExcelTool(InputStream fis, boolean isXlsx, String pwd) {
if (isXlsx) {
checkVersion();
impl = new XlsxImporter(fis, pwd);
} else {
impl = new XlsImporter(fis, pwd);
}
}
/**
* Get the maximum number of rows
*
* @return
*/
public int getMaxLineCount() {
return impl.getMaxLineCount();
}
/**
* Write a row of data
*
* @param items
* Object[]
*/
public void writeLine(Object[] items) throws IOException {
if (impl != null)
impl.writeLine(items);
}
/**
* Read a row of data
*
* @param items
* Object[]
*/
public Object[] readLine() throws IOException {
if (impl != null) {
return impl.readLine();
}
return null;
}
/**
* Skip line
*/
public boolean skipLine() throws IOException {
return impl.readLine() != null;
}
/**
* Complete the read and write operations.
*/
public void output() {
if (impl != null) {
impl.output();
}
}
/**
* Set the name of the sheet to be operated
*
* @param name
*/
public void setSheet(String name) {
if (impl != null) {
impl.setSheet(name);
}
}
/**
* Set the sequence number of the sheet to be operated.
*
* @param index
* Start from 1
*/
public void setSheet(int index) {
if (impl != null) {
impl.setSheet(index);
}
}
/**
* Total number of rows
*
* @return
*/
public int totalCount() {
if (impl != null) {
return impl.totalCount();
}
return 0;
}
/**
* Set start line
*
* @param start
*/
public void setStartRow(int start) {
if (impl != null) {
impl.setStartRow(start);
}
}
/**
* Set the number of rows to be fetched
*
* @param fetchCount
*/
public void setFetchCount(int fetchCount) {
if (impl != null) {
impl.setFetchCount(fetchCount);
}
}
/**
* Close
*/
public void close() throws IOException {
output();
}
/**
* Set the string to the cell of the Excel file. The string can be separated
* by return and tabs, and filled in adjacent rows and columns respectively.
* When sheet is omitted, it is the first page.
*
* @param fo
* FileObject
* @param isXlsx
* true means xlsx format, false means xls format.
* @param s
* Sheet name(If omitted, it is the first page)
* @param c
* Start cell
* @param t
* The string can be separated by return and tabs
* @param isRowInsert
* Whether to insert row
*/
public static void pasteXls(FileObject fo, boolean isXlsx, Object s,
String c, String t, boolean isRowInsert, String pwd) {
CellLocation pos = CellLocation.parse(c);
if (pos == null) {
throw new RQException(AppMessage.get().getMessage(
"excel.invalidcell", c));
}
/* If the string is empty, set the cell to empty. */
Matrix data = ExcelUtils.getStringMatrix(t, true);
if (isXlsx) {
new XlsxPaste(fo, s, pos, data, isRowInsert, pwd).output();
} else {
new XlsPaste(fo, s, pos, data, isRowInsert, pwd).output();
}
}
/**
* Used in function: f.pastexls(c:s,t)
*
* @param fo
* FileObject
* @param isXlsx
* true means xlsx format, false means xls format.
* @param s
* Sheet name(If omitted, it is the first page)
* @param c
* Start cell
* @param t
* The string can be separated by return and tabs
* @param pwd
* Excel password
* @return
*/
public static String pasteXlsRead(FileObject fo, boolean isXlsx, Object s,
String c, String t, String pwd) {
CellLocation startPos = CellLocation.parse(c);
if (startPos == null) {
throw new RQException(AppMessage.get().getMessage(
"excel.invalidcell", c));
}
CellLocation endPos = null;
if (StringUtils.isValidString(t)) {
endPos = CellLocation.parse(t);
if (endPos == null) {
throw new RQException(AppMessage.get().getMessage(
"excel.invalidcell", t));
}
}
int r1 = startPos.getRow();
int c1 = startPos.getCol();
if (endPos != null) {
int r2 = endPos.getRow();
int c2 = endPos.getCol();
startPos = new CellLocation(Math.min(r1, r2), Math.min(c1, c2));
endPos = new CellLocation(Math.max(r1, r2), Math.max(c1, c2));
}
int startRow = startPos.getRow();
InputStream fis = null;
try {
ExcelTool et = new ExcelTool(fis, isXlsx, pwd);
if (s instanceof String) {
et.setSheet((String) s);
} else if (s instanceof Number) {
et.setSheet(((Number) s).intValue() - 1);
} else if (s != null) {
MessageManager mm = EngineMessage.get();
throw new RQException("xlspaste"
+ mm.getMessage("function.paramTypeError"));
}
et.setStartRow(startRow);
Object[] line = et.readLine();
if (line == null || line.length == 0) {
return null;
}
int endCol = -1;
if (endPos != null) {
endCol = endPos.getCol();
} else {
endCol = line.length - 1;
}
int startCol = startPos.getCol();
int colCount = endCol - startCol + 1;
List