SSH上传下载功能

cardManageAction中的上传下载主要功能:

private InputStream excelFile;

private File uploadFile;
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
private String uploadFileFileName;


//导出数据
public String exportExcelInto() throws Exception {
{
HttpServletRequest request = ServletActionContext.getRequest();
String ids = request.getParameter("ids");
List<Card> list = new ArrayList<Card>();
String[] array = ids.split(",");
int[] id = new int[array.length];
for (int i = 0; i < id.length; i++) {
Card card = cardSer.findByCardId(Integer.valueOf(array[i]));
list.add(card);
}
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("储值卡信息");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("储值卡余额");
// row.createCell(2).setCellValue("年龄");
// row.createCell(3).setCellValue("性别");
// row.createCell(4).setCellValue("地址");
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
for (int i = 1; i <= list.size(); i++) {
Card c = list.get(i - 1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(c.getCardId());
row.createCell(1).setCellValue(c.getBalance());
// row.createCell(2).setCellValue(stu.getS_age());
// row.createCell(3).setCellValue(stu.getS_sex());
// row.createCell(4).setCellValue(stu.getS_address());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
excelFile = new ByteArrayInputStream(baos.toByteArray());
baos.close();
return "success";
}
}

// 导入Excel
public String ExcelInto() throws Exception {
String directory = "/file";
String targetDirectory = ServletActionContext.getServletContext()
.getRealPath(directory);
File target = UploadFile.Upload(uploadFile, uploadFileFileName,
targetDirectory);
List<Card> sList = new ArrayList<Card>();
excelFile = new FileInputStream(target);
Workbook wb = new HSSFWorkbook(excelFile);
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum() + 1;
for (int i = 1; i < rowNum; i++) {
Card card = new Card();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for (int j = 0; j < cellNum; j++) {
Cell cell = row.getCell(j);
String cellValue = null;
switch (cell.getCellType()) { // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库
case 0:
cellValue = String
.valueOf((int) cell.getNumericCellValue());
break;
case 1:
cellValue = cell.getStringCellValue();
break;
case 2:
cellValue = String
.valueOf((int) cell.getNumericCellValue());
break;
case 3:
cellValue = cell.getStringCellValue();
break;
case 4:
cellValue = cell.getStringCellValue();
break;
}

switch (j) {   // 通过列数来判断对应插如的字段
case 0:
card.setCardId(Integer.valueOf(cellValue));
break;
case 1:
card.setBalance(Long.valueOf(cellValue));
break;
}
}
sList.add(card);
}
cardSer.add(sList);
return "pageAction";

}

Service层:

public int add(List<Card> cards) {
if(cards.size() > 0){
int sCard = cards.size();
for(int i=0;i<sCard;i++){

this.addCard(cards.get(i));
}
return 1;
}
return 0;
}

public int addCard(Card card) {
CardDAO carddao = new CardDAO();
Transaction tr = carddao.getSession().beginTransaction();
carddao.save(card);
tr.commit();
carddao.getSession().close();
return 0;
}


JSP层:

  <form action="cardManageActionExcelInto" enctype="multipart/form-data"
method="post">
<table width="100%" border="0" align="center">
<tr>
<td colspan="99" id="more"><input type="file"
name="uploadFile" id="uploadFile" /> <input type="submit"
value="上传" /> <input type="reset" value="重置" /></td>
</tr>
</table>
</form>
<div  style='text-align:center'>
<a href="javascript:" id="excel"><img border="0" src="card/images/excel.png"/>导出数据</a> 
</div>

JavaScript

function xuanzhong(){
var str=document.getElementsByName("box");
if(!!$("#checkbox").attr("checked")){
for(var i=0;i<str.length;i++){
str[i].checked=true;
}
}else{
for(var i=0;i<str.length;i++){
str[i].checked=false;
}
}
}
  
   var ids='';
$(function(){
$("#excel").click(function(){
$("#tab input[type='checkbox']").each(function(){
if($(this).is(":checked"))
{
if($(this).val()!='on')
{
ids=ids+$(this).val()+',';
}
}
});
if(ids==''){
alert("请选择要导出的数据!");
}else{
if(confirm("确认导出选中数据吗?")){
document.forms[0].action="cardManageActionexportExcelInto?ids="+ids;
document.forms[0].submit();
ids='';
}else{
//将复选框的状态更改为位选中,并清空所有的Id数值
$.each($("#tab input[type='checkbox']"),function(i,v){if($(v).attr("checked")=="checked"){ v.checked=false;}});ids='';
}
}
});
}); 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值