JAVA 数据导入Excel
2009-12-03 19:49
369 查看
<!---Action调用---!>
public String makeExcel()throws ApplicationException{
this.getDownloadFile();
return SUCCESS;
}
public InputStream getDownloadFile()
throws ApplicationException{
shpShopDto = (ShpShopDto) this.getSession().get("shpShopDtoSession");
if(shpShopDto!=null){
Pagination pagination = new Pagination(pageNo, rowsPerPage);
InputStream is=ServiceManager.agent.getShpSecModelService().makeExcel(shpShopDto.getCode(), pagination);
getRequest().setAttribute("pagination", pagination);
return is;
}else{
return null;
}
}
<!---Service相关业务---->
public InputStream makeExcel(Long shopcode,Pagination
pagination) throws ApplicationException {
HSSFWorkbook
book=new HSSFWorkbook();
HSSFSheet
sheet=book.createSheet("sheet1");
HSSFRow
row=sheet.createRow(0);
HSSFCell cell=row.createCell((short)
0);
cell.setCellValue("编号");
cell=row.createCell((short) 1);
cell.setCellValue("名称");
cell=row.createCell((short) 2);
cell.setCellValue("金额");
cell=row.createCell((short)3);
cell.setCellValue("开始日期");
cell=row.createCell((short)4);
cell.setCellValue("结束日期");
cell=row.createCell((short)5);
cell.setCellValue("数量");
//list为要生成Excel的数据的集合
List
list=shpSecModelDao.find(shopcode, pagination);
if(list.size()>0){
for(int i=0;i<list.size();i++){
ShpSecModelDto secModel=(ShpSecModelDto)list.get(i);
row=sheet.createRow(i+1); //i=0,有个list(0),有一行数据
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
cell=row.createCell((short)0);
cell.setCellValue(secModel.getId());
cell=row.createCell((short)1);
cell.setCellValue(secModel.getName());
cell=row.createCell((short)2);
cell.setCellValue(secModel.getAmount());
cell=row.createCell((short)3);
cell.setCellValue(sf.format(secModel.getStarttime()));
cell=row.createCell((short)4);
cell.setCellValue(sf.format(secModel.getEndtime()));
cell=row.createCell((short)5);
cell.setCellValue(secModel.getAllquantity());
}
}
ByteArrayOutputStream arrayIo=new ByteArrayOutputStream();
try {
book.write(arrayIo);
} catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[]
by=arrayIo.toByteArray();
InputStream is = new
ByteArrayInputStream(by);
if(arrayIo !=null){
try {
arrayIo.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
return is;
}
<!---XML配置--!>
<action name="generateExcel"
class="com.fzdna.application.agent.actions.shop.manage.ShpSecuritiesAction" method="makeExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="goodNumbers.xls" </param>
<param name="inputName">downloadFile
</param>
<!--downloadFile必须配置成事务拦截,downloadFile必须和Action的方法名对应,如getdownloadFile()
-->
</result>
</action>
public String makeExcel()throws ApplicationException{
this.getDownloadFile();
return SUCCESS;
}
public InputStream getDownloadFile()
throws ApplicationException{
shpShopDto = (ShpShopDto) this.getSession().get("shpShopDtoSession");
if(shpShopDto!=null){
Pagination pagination = new Pagination(pageNo, rowsPerPage);
InputStream is=ServiceManager.agent.getShpSecModelService().makeExcel(shpShopDto.getCode(), pagination);
getRequest().setAttribute("pagination", pagination);
return is;
}else{
return null;
}
}
<!---Service相关业务---->
public InputStream makeExcel(Long shopcode,Pagination
pagination) throws ApplicationException {
HSSFWorkbook
book=new HSSFWorkbook();
HSSFSheet
sheet=book.createSheet("sheet1");
HSSFRow
row=sheet.createRow(0);
HSSFCell cell=row.createCell((short)
0);
cell.setCellValue("编号");
cell=row.createCell((short) 1);
cell.setCellValue("名称");
cell=row.createCell((short) 2);
cell.setCellValue("金额");
cell=row.createCell((short)3);
cell.setCellValue("开始日期");
cell=row.createCell((short)4);
cell.setCellValue("结束日期");
cell=row.createCell((short)5);
cell.setCellValue("数量");
//list为要生成Excel的数据的集合
List
list=shpSecModelDao.find(shopcode, pagination);
if(list.size()>0){
for(int i=0;i<list.size();i++){
ShpSecModelDto secModel=(ShpSecModelDto)list.get(i);
row=sheet.createRow(i+1); //i=0,有个list(0),有一行数据
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
cell=row.createCell((short)0);
cell.setCellValue(secModel.getId());
cell=row.createCell((short)1);
cell.setCellValue(secModel.getName());
cell=row.createCell((short)2);
cell.setCellValue(secModel.getAmount());
cell=row.createCell((short)3);
cell.setCellValue(sf.format(secModel.getStarttime()));
cell=row.createCell((short)4);
cell.setCellValue(sf.format(secModel.getEndtime()));
cell=row.createCell((short)5);
cell.setCellValue(secModel.getAllquantity());
}
}
ByteArrayOutputStream arrayIo=new ByteArrayOutputStream();
try {
book.write(arrayIo);
} catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[]
by=arrayIo.toByteArray();
InputStream is = new
ByteArrayInputStream(by);
if(arrayIo !=null){
try {
arrayIo.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
return is;
}
<!---XML配置--!>
<action name="generateExcel"
class="com.fzdna.application.agent.actions.shop.manage.ShpSecuritiesAction" method="makeExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="goodNumbers.xls" </param>
<param name="inputName">downloadFile
</param>
<!--downloadFile必须配置成事务拦截,downloadFile必须和Action的方法名对应,如getdownloadFile()
-->
</result>
</action>
相关文章推荐
- Java将数据库数据导入EXCEL
- 如何用Java把数据从数据库导入到Excel中
- Java导入Excel表格,并将数据存入数组中
- Java 使用jxl.jar 导入导出Excel数据
- Java:将Excel数据导入到数据库(一眼就看会)
- java将数据库查询数据导入excel
- JAVA通过poi对Excel数据在(jsp+ssh)环境下导入导出
- JAVA中excel表数据的批量导入
- Java 利用poi把数据库中数据导入Excel
- 用java导入Excel数据到数据库
- java将表数据导入excel
- 小例子:java利用poi读取excel中数据并导入数据库
- java实现灵活导入excel数据
- JAVA操作oracle将数据导入Excel
- 用java将excel中数据导入mysql
- Java中使用Apache POI实现数据的Excel导入和导出
- java中使用POI导入excel数据的过程中,遇到读取以科学计数法显示的数据
- Java之excel简单导入数据
- JAVA EXCEL 数据 导入 ORACLE
- JAVA WEB 实现文件数据导入(CSV、TXT、excel)