/*
* Decompiled with CFR 0_101.
*
* Could not load the following classes:
* org.apache.poi.hssf.usermodel.HSSFWorkbook
* org.apache.poi.poifs.filesystem.POIFSFileSystem
* org.apache.poi.ss.usermodel.Cell
* org.apache.poi.ss.usermodel.CellStyle
* org.apache.poi.ss.usermodel.DataFormat
* org.apache.poi.ss.usermodel.Row
* org.apache.poi.ss.usermodel.Sheet
* org.apache.poi.ss.usermodel.Workbook
*/
package com.enation.framework.util;
import com.enation.framework.util.StringUtil;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
// 直接在上班项目中导出 没测试
public class ExcelUtil {
private Workbook wb = new HSSFWorkbook();
private POIFSFileSystem fs = null;
public void openModal(InputStream in) {
try {
this.fs = new POIFSFileSystem(in);
this.wb = new HSSFWorkbook(this.fs);
}
catch (IOException e) {
e.printStackTrace();
}
}
public boolean openModal(String modlePath) {
Object In = null;
Object File_in = null;
try {
this.fs = new POIFSFileSystem((InputStream)new FileInputStream(modlePath));
this.wb = new HSSFWorkbook(this.fs);
return true;
}
catch (IOException e) {
e.printStackTrace(System.err);
return false;
}
}
public void writeToFile(String targetFile) {
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(targetFile);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
this.wb.write((OutputStream)fileOut);
}
catch (IOException e1) {
e1.printStackTrace();
}
finally {
this.wb = null;
try {
fileOut.flush();
fileOut.close();
}
catch (IOException e2) {
e2.printStackTrace();
}
}
}
public void setCellMoney(int numRow, int numCol) {
try {
if (this.wb.getSheetAt(0) != null) {
Sheet aSheet = this.wb.getSheetAt(0);
DataFormat format = this.wb.createDataFormat();
Row row = aSheet.getRow((short)numRow);
Cell csCell = row.getCell((short)numCol);
CellStyle style = this.wb.createCellStyle();
style.setDataFormat(format.getFormat("0.00"));
style.setBorderBottom((short)1);
style.setBottomBorderColor((short)8);
style.setBorderLeft((short)1);
style.setLeftBorderColor((short)8);
style.setBorderRight((short)1);
style.setRightBorderColor((short)8);
style.setBorderTop((short)1);
style.setTopBorderColor((short)8);
csCell.setCellStyle(style);
}
}
catch (Exception e) {
// empty catch block
}
}
public void writeStringToCell(int numRow, int numCol, String strval) {
try {
String string = strval = StringUtil.isEmpty(strval) ? "" : strval;
if (this.wb.getSheetAt(0) != null) {
Cell csCell;
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.getRow((int)((short)numRow));
if (row == null) {
row = aSheet.createRow(numRow);
}
if ((csCell = row.getCell((int)((short)numCol))) == null) {
csCell = row.createCell(numCol);
}
csCell.setCellValue(strval);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void insertStringToCell(int numRow, int numCol, String strval) {
try {
if (this.wb.getSheetAt(0) != null) {
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.createRow((int)((short)numRow));
Cell csCell = row.createCell((int)((short)numCol));
csCell.setCellValue(strval);
}
}
catch (Exception e) {
// empty catch block
}
}
public void writeFormula(int numRow, int numCol, String formula) {
try {
if (this.wb.getSheetAt(0) != null) {
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.getRow((int)((short)numRow));
Cell csCell = row.getCell((int)((short)numCol));
csCell.setCellFormula(formula);
}
}
catch (Exception e) {
// empty catch block
}
}
public void insertFormula(int numRow, int numCol, String formula) {
try {
if (this.wb.getSheetAt(0) != null) {
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.createRow((int)((short)numRow));
Cell csCell = row.createCell((int)((short)numCol));
csCell.setCellFormula(formula);
}
}
catch (Exception e) {
// empty catch block
}
}
public void writeNumToCell(int numRow, int numCol, Double num) {
try {
if (this.wb.getSheetAt(0) != null) {
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.getRow((int)((short)numRow));
Cell csCell = row.getCell((int)((short)numCol));
csCell.setCellValue(num.doubleValue());
}
}
catch (Exception e) {
// empty catch block
}
}
public void insertNumToCell(int numRow, int numCol, Double num) {
try {
if (this.wb.getSheetAt(0) != null) {
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.createRow((int)((short)numRow));
Cell csCell = row.createCell((int)((short)numCol));
csCell.setCellValue(num.doubleValue());
}
}
catch (Exception e) {
// empty catch block
}
}
public void replaceDataToCell(int numRow, int numCol, String temstr, String strval) {
try {
if (this.wb.getSheetAt(0) != null) {
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.getRow((int)((short)numRow));
Cell csCell = row.getCell((int)((short)numCol));
String temp = "";
temp = csCell.getStringCellValue();
temp = temp.replaceAll(temstr, strval);
csCell.setCellValue(temp);
}
}
catch (Exception e) {
// empty catch block
}
}
public void insertDataToExcel(int numRow, Object[] object) {
try {
if (null != this.wb.getSheetAt(0)) {
Sheet aSheet = this.wb.getSheetAt(0);
Row row = aSheet.getRow((int)((short)numRow));
if (row == null) {
row = aSheet.createRow((int)((short)numRow));
}
for (int i = 0; i < object.length; ++i) {
Cell csCell = row.createCell((short)i);
CellStyle style = this.wb.createCellStyle();
style.setBorderBottom((short)1);
style.setBottomBorderColor((short)8);
style.setBorderLeft((short)1);
style.setLeftBorderColor((short)8);
style.setBorderRight((short)1);
style.setRightBorderColor((short)8);
style.setBorderTop((short)1);
style.setTopBorderColor((short)8);
csCell.setCellStyle(style);
if (object[i] != null)
csCell.setCellValue(object[i].toString());
else
csCell.setCellValue("0");
}
}
}
catch (Exception e) {
// empty catch block
}
}
public int getExcelLastCellNum() {
int count = 0;
if (null != this.wb.getSheetAt(0)) {
Sheet aSheet = this.wb.getSheetAt(0);
Row aRow = aSheet.getRow(0);
count = aRow.getLastCellNum();
}
return count;
}
public String getOutputPath(String str) {
String temp = "";
temp = str.lastIndexOf("/") != -1 ? str.substring(0, str.lastIndexOf("/")) : (str.lastIndexOf("\\") != -1 ? str.substring(0, str.lastIndexOf("\\")) : (str.lastIndexOf("\\\\") != -1 ? str.substring(0, str.lastIndexOf("\\\\")) : str));
return temp;
}
public Row getRow(int sheetIndex, int rowIndex) {
Sheet sheet = this.wb.getSheetAt(sheetIndex);
return sheet.getRow(rowIndex);
}
public Sheet getSheet(int sheetIndex) {
return this.wb.getSheetAt(sheetIndex);
}
public Sheet getSheet(String sheetName) {
return this.wb.getSheet(sheetName);
}
public static Object getCellValue(Cell cell) {
if (cell == null) {
return null;
}
int celltype = cell.getCellType();
if (celltype == 0) {
return cell.getNumericCellValue();
}
if (celltype == 1) {
String value = cell.getStringCellValue();
if ("null".equals(value)) {
value = "";
}
if (!StringUtil.isEmpty(value)) {
value = value.replaceAll("\u00a0", "");
value = value.replaceAll("\u00a0 ", "");
}
return value;
}
if (celltype == 2) {
String value = cell.getStringCellValue();
if ("null".equals(value)) {
value = "";
}
return value;
}
if (celltype == 3) {
return "";
}
if (celltype == 5) {
return "";
}
return "";
}
// 测试方法
public static void main(String[] args) {
int i = 1;
for (orderMap order : orderList) {
excelUtil.writeStringToCell(i, 0, order.getSame());
i++;
}
}
分享到:
相关推荐
导入根据表头自动获取excel表格数据--------------若依框架里有
springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel ...
软件是通过EXCEL编制的,可以自动生成X-MR控制图,与其他的EXCEL制作的X-MR控制图软件相比,该网站提供的Excel版本的X-MR控制图软件更加专业,更加好用. 不过,对于国内的SPC软件研究与学习的人来说,使用该软件唯一的...
Excel财务软件-从凭证输入到报表生成
Excel财务软件-从凭证输入到报表生成
excel VBA - 排列组合生成算法 - ,可快速生成指定项目的所有排列组合
ASP.NET使用Excel的Application组件生成Excel --------------------------------------------------- 开发测试环境: Windows Server 2003 VS.NET 2005 access Microsoft Office 2003 其它说明: 1、ExcelClass类...
NPOI 是 POI 项目的 .NET 版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。...NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。
这是我自己写的一个c#操作excel的例子,其中包括数据操作,字体,格式,单元格等操作,还有一个生成柱状图的方法,所有的方法都在ExcelHelper.cs类里,点击Form1窗体里的按钮可以看到各种操作的例子,生成的excel在...
EXCEL常用模板-人事模板-工资类-工资条生成工具
Excel模板27-单选自动生成图表(模板).zip
EXCEL常用模板-人事模板-工资类-工资表生成工资条
使用VC/C++生成excel的方式有很多,但是绝大部分要么是简单的csv文件,要么需要COM并已经安装ms excel,局限性比较大。 我这里提供一种使用C++编写以html脚本的方式生成xls文件的方法。不使用任何非C++标准库,支持...
一款完全免费的将MARC格式转为EXCEL的工具--PxMarcToExcel (1) 支持多文件批量转换,批量修改输出路径 (2) 支持大文件转换 (3) 可选提取字段 (4) 本地机器可不需要安装EXCEL (5) 当提取字段同名有多个时,可以选择提取...
EXCEL常用模板-人事模板-考勤类自动生成考勤表
excel生成sql脚本
exceleasy模板生成excel
详情:可以通过客户给的Excel矩阵表通过配置生成LDF文件和位定义文件,也可以通过客户给的LDF文件生成Excel矩阵表和位定义文件, 附件里有demo模板,可自己创建配置ini文件。 具体使用说明:...
Python代码,实现对存储在Excel表的成绩数据自动分析并生成图表。成绩对应期末列。