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='';
}
}
});
}); 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章