package com.xsq.IRP.balanceManage.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.eclipse.jdt.internal.compiler.flow.FinallyFlowContext;
import com.xsq.IRP.cardManage.dao.CardManageDaoFactory;
import com.xsq.IRP.cfgManage.dao.CfgDaoFactory;
import com.xsq.IRP.cfgManage.service.CfgManageFactory;
import com.xsq.IRP.connpool.ConnManager;
import com.xsq.IRP.connpool.ConnectionPool;
import com.xsq.IRP.entity.Customer;
import com.xsq.IRP.entity.Fee;
import com.xsq.IRP.entity.FeeFang;
import com.xsq.IRP.entity.FeeType;
import com.xsq.IRP.entity.Flow;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ReadXLS {
HashMap<String, List<Fee>> hash=new HashMap();
List<Fee> feefang = new ArrayList<Fee>();
/** Excel文件存放位置。注意是正斜线
* 一个Excel文件的层次:Excel文件->工作表->行->单元格 对应到POI中,为:workbook->sheet->row->cell
* */
//public static String fileToBeRead = "D:/stb_Account1.xls";
@SuppressWarnings("deprecation")
DecimalFormat df = new DecimalFormat("#");
List<Fee> fee=new ArrayList<Fee>();
ConnManager manager = ConnectionPool.getConnManager();
public HashMap ReadExcle(String fileToBeRead)
{
System.out.println("second");
int result=0;
List str=new ArrayList();
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); //创建一个Excel工作表的引用
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
String strCell="";
HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
//循环遍历一个sheet,生成row对象
for (int rowNumOfSheet = 1; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
//循环遍历一个row,生成单元格cell对象
for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);
int cellType = aCell.getCellType();
//System.out.println(cellType);
switch (cellType) {
case 0://数字
strCell += df.format(aCell.getNumericCellValue())+",";
//System.out.println(strCell);
break;
case 1://字符
strCell+= aCell.getStringCellValue()+",";
// System.out.println(strCell);
break;
}
}
}
}
}
//System.out.println(strCell);
String[]strs=strCell.split(",");
int len=(strs.length)/3;
int k=0;
int h=0;
try{
for(int i=0;i<len;i++)
{
Fee ff=new Fee();
String[]ff1=new String[3];
for(int j=0;j<3;j++)
{
h=k*3+j;
ff1[j]=strs[h];
}
ff.setUserCode(ff1[0]);
ff.setFeeTypeName(ff1[1]);
ff.setFeeMoney(new Double(ff1[2]));
fee.add(ff);
k++;
}
}catch(Exception e)
{
System.out.println("有错");
}
}
for(Fee f:fee)
{
Fee myfee=new Fee();
Customer c = new Customer();
FeeType fee=new FeeType();
try {
c=CardManageDaoFactory.getCustomerDao(manager).findCustomerInfoByUserCode(f.getUserCode());
int accountNo=CardManageDaoFactory.getAccountDao(manager).findAccountByUserCode(f.getUserCode());
fee=CfgDaoFactory.getFeeTypeDao(manager).findByName(f.getFeeTypeName());
if(c.getUserName()==null)
{
break;
}
if(fee.getFeeTypeName()==null)
{
break;
}
myfee.setAccountNo(accountNo);
myfee.setUserCode(c.getUserCode());
myfee.setUserName(c.getUserName());
myfee.setOrgName(c.getOrgName());
myfee.setFeeTypeName(fee.getFeeTypeName());
myfee.setFeeType((short) fee.getFeeTypeID());
myfee.setFeeMoney(f.getFeeMoney());
feefang.add(myfee);
} catch (SQLException e) {
//e.printStackTrace();
// TODO Auto-generated catch block
result=0;
break;
}
result=1;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
if(result==1)
{
hash.put("1", feefang);
return hash;
}
else
{
return null;
}
}
public String Classpath()
{
String classpath=ClassLoader.getSystemResource("").toString();
classpath=classpath.substring(classpath.indexOf(":")+2, classpath.length()-1);
System.out.println(classpath);
return classpath;
}
public static void main(String[] args) {
// DisplayFather df = new DisplayFather();
// StringBuffer test = df.returnFa("012122");
//ReadXLS rxl=new ReadXLS();
//Date date = new java.util.Date();
//int s= date.getDate();
//System.out.println(s);
//String fileToBeRead="D:/stb_Account1.xls";
//rxl.ReadExcle(fileToBeRead);
// rxl.Classpath();
try{
File delfile = new File("D:\\tomcat-5.5.17\\webapps\\IRP\\WEB-INF\\classes\\stb_Account1.xls");
delfile.delete();
System.out.println("删除文件成功");
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java导出Excel文件
共9个文件
jar:4个
java:2个
xls:2个
5星 · 超过95%的资源 需积分: 10 146 下载量 154 浏览量
2008-11-11
18:02:29
上传
评论 1
收藏 1012KB RAR 举报
温馨提示
这是一个很通用的导出文件示例,希望它是你最保贵的资料,加油吧...欢迎下载.
资源推荐
资源详情
资源评论
收起资源包目录
Java导出Excel文件.rar (9个子文件)
ReadXLS.java 6KB
exportExcel.jsp 2KB
导出Excel
需要用到的Jar包
excelutils.jar 35KB
poi-3.0.1.jar 888KB
tm-extractors-0.4.jar 226KB
commons-io-1.1.jar 60KB
picExcel.java 4KB
需用到的xls文件
PicStat.xls 39KB
statPic.xls 37KB
共 9 条
- 1
资源评论
- wangyudiwang2011-11-12没有现成的例子啊
- pwpal2012-10-19参考了这个程序 最后实现了自己的程序,谢谢分享哈。!
jeansan
- 粉丝: 4
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功