import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.zbiti.core.components.db.DBQuery;
import com.zbiti.core.components.db.DDProxy;
import com.zbiti.core.util.db.DBConnection;
import com.zbiti.core.vo.ParameterObject;
import ictpm.bzj_clean.component.WriteToModelExcel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class IctJzIncomeCostExportTable {
int startrow = 2;
int startcol = 1;
String cols = null;
DBConnection dbc = null;
DBQuery dbq = null;
ParameterObject po = null;
String exportPath;
public IctJzIncomeCostExportTable(DBConnection dbc, ParameterObject po, String exportPath) {
this.dbc = dbc;
this.dbq = new DBQuery(dbc);
this.po = po;
this.exportPath = exportPath;
this.cols = "YEAR_MONTH_ALL,JI_COMPANY_NAME,JI_PUB_CODE,JI_NAME,JI_IS_CL,JI_TYPE,JI_STATION_TYPE,JI_TOWER_PROPERTY," +
"JI_PLAN_CODE,JI_TP_SITE_NUM,JI_SITE_NUM,JI_CODE_NUM,JI_LONGITUDE,JI_LAITUDE,JI_JZZJ," +
"RRUNAME,NODE_TYPE,P_ID,ANLKL,NAFAZ";
}
public IctJzIncomeCostExportTable() {
}
@SuppressWarnings("unchecked")
public int write(Map<String, String> params) {
boolean is_success = true;
List<Map<String, String>> list =new ArrayList<Map<String, String>>();
try {
DDProxy ddp = new DDProxy(dbc, "JZ_INCOME_COST_QUERY");
ddp.getDBQuery().setNoneConditionError(false);
this.dbc.setExBigData(false);
list = dbq.query("JZ_INCOME_COST_QUERY.import", params);
WriteToModelExcel wtme = new WriteToModelExcel();
boolean flag = writeToExcel(exportPath, startrow, startcol, list, cols);
} catch (SQLException e) {
e.printStackTrace();
}
return list.size();
}
/**
* 将list中指定的字段填入excel模板
*
* @param exportPath excel模板所在位置路径
* @param startRow 数据填入起始行
* @param startCol 数据填入起始列
* @param list 所需填入的list
* @param cols 指定的字段
* @author liukang
*/
public boolean writeToExcel(String exportPath, int startRow, int startCol, List<Map<String, String>> list, String cols) {
FileInputStream fis = null;
FileOutputStream fos = null;
SXSSFWorkbook sxssfWorkbook = null;
XSSFWorkbook wb=null;
try {
fis = new FileInputStream(exportPath);
wb = new XSSFWorkbook(fis);
sxssfWorkbook=new SXSSFWorkbook(wb,2000);
Sheet sheet = sxssfWorkbook.getSheetAt(0);
String[] list_cols = cols.split(",");
for(int i = 0;i < list.size();i++){
Row row = null;
if(sheet.getRow(startRow+i-1) != null){
row = sheet.getRow(startRow+i-1);
}else{
row = sheet.createRow(startRow+i-1);
}
for(int j = 0;j < list_cols.length;j++){
Cell cell = null;
if(row.getCell(j+startCol-1) != null){
cell = row.getCell(j+startCol-1);
}else{
cell = row.createCell(j+startCol-1);
}
cell.setCellValue(list.get(i).get(list_cols[j]));
}
}
fos = new FileOutputStream(exportPath);
sxssfWorkbook.write(fos);
// wb.write(fos);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (null != sxssfWorkbook) {
sxssfWorkbook.dispose();
}
if (null != fis) {
fis.close();
}
if (null != fos) {
fos.flush();
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 压缩文件(文件夹)
*
* @param path 目标文件流
* @param format zip 格式 | rar 格式
* @throws Exception
*/
public String zipFile(File path, String format) throws Exception {
String generatePath = "";
if (path.isDirectory()) {
generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator + path.getName() + "." + format : path.getParent() + path.getName() + "." + format;
} else {
generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator : path.getParent();
generatePath += path.getName().substring(0, path.getName().lastIndexOf(".")) + "." + format;
}
// 输出流
FileOutputStream outputStream = new FileOutputStream(generatePath);
// 压缩输出流
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
out.setEncoding("gbk");
zip(out, path, "");
out.flush();
out.close();
return generatePath;
}
/**
* 递归压缩文件
*
* @param output ZipOutputStream 对象流
* @param file 压缩的目标文件流
* @param childPath 条目目录
*/
private void zip(ZipOutputStream output, File file, String childPath) {
FileInputStream input = null;
try {
// 文件为目录
if (file.isDirectory()) {
// 得到当前目录里面的文件列表
File list[] = file.listFiles();
childPath = childPath + (childPath.length() == 0 ? "" : "/")
+ file.getName();
// 循环递归压缩每个文件
for (File f : list) {
zip(output, f, childPath);
}
} else {
// 压缩文件
childPath = (childPath.length() == 0 ? "" : childPath + "/")
+ file.getName();
output.putNextEntry(new ZipEntry(childPath));
output.setEncoding("gbk");
input = new FileInputStream(file);
int readLen = 0;
byte[] buffer = new byte[1024 * 8];
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {
output.write(buffer, 0, readLen);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 关闭流
if (input != null) {
try {
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
两种方法
1 用符号代替: > gt , >= gte ,< lt , <= lte
2 加括号 <#if(x>y)>