从jsp页面上传 excel文件 存到数据库 用json做

从jsp页面上传 excel文件 存到数据库 用json做,第1张

不明白为啥指定json,我的建议是:

1、STRUTS文件标签选择并上传EXCEL文件,示例代码:

<html:form action="/uploadExcelFile/UploadExcelFileAction.go" enctype="multipart/form-data" method="post" onsubmit="return confirm_save()">

<table border="0" cellpadding="0" cellspacing="5" align="center" style="margin:50">

<tr>

<td align="right" nowrap >请选择Excel文件(.<%=excelType%>):</td>

<td align="right" colspan="3" valign="middle" nowrap ><html:file property="aircraftExcel" size="200" style="width:400height:25" value=""></html:file></td>

</tr><tr>

<td></td>

<td align="right">Excel文件中标题(表头)占行:</td>

<td align="left"><input name="topicRow" value="0" style="width:50" onKeyUp="value=value.replace(/[\D]/g,'')"/></td>

<TD align="right" nowrap title="上传"><html:submit value="开始更新" style="height:25width:70"></html:submit></TD>

</tr>

</table>

</html:form>

2、UploadExcelFileAction接收、写入后台指定目录(局部代码)

FileOutputStream fos = null

try {

//得到文件数据(数组)

byte[] fileDate = ff.getFileData()

//得到服务器硬盘文件夹的绝对路径

ServletContext application = this.getServlet().getServletContext()

String realPath = application.getRealPath("/")

if(excelType==null||excelType.equals("xlsx"))//默认生成2007以后版本格式

fos = new FileOutputStream(realPath+"aircraftExcelTable.xlsx")

else

fos = new FileOutputStream(realPath+"aircraftExcelTable.xls")

fos.write(fileDate)

fos.close()

if(excelType==null||excelType.equals("xlsx"))//默认生成2007以后版本格式

num=ReadExcelSaveToDB.ReadAndSaveNew(realPath+"aircraftExcelTable.xlsx",topicNumber)

else

num=ReadExcelSaveToDB.ReadAndSave(realPath+"aircraftExcelTable.xls",topicNumber)

if(num==-1){

session.setAttribute("forwardPage", currentPage)

session.setAttribute("prompt", "文件传输失败或无法连接数据库!")

return mapping.findForward("promptpage")

}else if(num==-2){

session.setAttribute("forwardPage", currentPage)

session.setAttribute("prompt", "Excel文件大于1000条或不能识别!重新制作Excel文件。")

return mapping.findForward("promptpage")

}else if(num==-3){

session.setAttribute("forwardPage", currentPage)

session.setAttribute("prompt", "Excel文件中包含非法字符(非数字),请修正后重试。")

return mapping.findForward("promptpage")

}

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

}finally{

try {

fos.close()

} catch (IOException e) {

e.printStackTrace()

}

}

3、解析读取表数据(POI)并存入数据库(局部代码)

public static int ReadAndSave(String path,String topicNumber){

try {

File file=new File(path)

FileInputStream fs=new FileInputStream(file)

@SuppressWarnings("resource")

HSSFWorkbook workbook=new HSSFWorkbook(fs)

//一共有几张表

int sheet_num=workbook.getNumberOfSheets()

setSheetNumber(0)

setProcess(0)//开始

//循环每个表

for(int i=0i<sheet_numi++){

HSSFSheet sheet=workbook.getSheetAt(i)

setSheetNumber(i+1)

if(sheet!=null&&sheet.getPhysicalNumberOfRows()!=0){

int topicNumberNum=Integer.parseInt(topicNumber)//越过首行的标题(如果有)

totalNumber=sheet.getPhysicalNumberOfRows()-topicNumberNum

//循环各个行

for(int j=sheet.getFirstRowNum()+topicNumberNumj<=sheet.getPhysicalNumberOfRows()j++){

HSSFRow row=sheet.getRow(j)

String aircraft=null

//String aircraftType=null

Double takeoffWeight=0.00

Double payloadMax2=0.00

int seatNumberMax2=0

if(row!=null&&row.getPhysicalNumberOfCells()!=0){

switch(row.getCell(0).getCellType()){

case Cell.CELL_TYPE_STRING:

aircraft=(row.getCell(0).getStringCellValue()).trim()

break

case Cell.CELL_TYPE_NUMERIC:

aircraft=String.valueOf(row.getCell(0).getNumericCellValue()).trim()

break

}

//继续处理其他单元格、行

//存入数据库

AircraftTableActionForm atb=new AircraftTableActionForm()

if(aircraft!=null&&!aircraft.equals("")){

atb.setAircraft(aircraft)

//set其他值

updateByExcel(atb)!=-1

}

jsp页面中可以通过动态生成excel的方式把table数据导入并保存。

以下是例子:

public static void crExcel(List list, String absoluteFileString)

throws Exception {

// 字体格式

WritableFont wfc0 = new WritableFont(WritableFont.ARIAL, 22,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK)

WritableCellFormat wcfFC0 = new WritableCellFormat(wfc0)

wcfFC0.setAlignment(jxl.format.Alignment.CENTRE)

wcfFC0.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE)

wcfFC0.setBorder(Border.ALL, BorderLineStyle.THIN,jxl.format.Colour.GRAY_25)

// 表头字体

WritableFont wfc8 = new WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK)

WritableCellFormat wcfFC8 = new WritableCellFormat(wfc8)

wcfFC8.setAlignment(jxl.format.Alignment.CENTRE)

wcfFC8.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE)

wcfFC8.setBorder(Border.ALL, BorderLineStyle.THIN,jxl.format.Colour.GRAY_25)

WritableFont wfc1 = new WritableFont(WritableFont.ARIAL, 10,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK)

WritableCellFormat wcfFC1 = new WritableCellFormat(wfc1)

wcfFC1.setAlignment(jxl.format.Alignment.CENTRE)

wcfFC1.setBorder(Border.ALL, BorderLineStyle.THIN,jxl.format.Colour.GRAY_25)

// 创建excel

OutputStream os = new FileOutputStream(absoluteFileString)

String readPath = I18nMessages.getText("efine.excel.dir")

InputStream input = new FileInputStream(readPath

+ "FKBalanceReport.xls")

Workbook workbook = Workbook.getWorkbook(input)

WritableWorkbook book = Workbook.createWorkbook(os, workbook)

WritableSheet sheet = book.getSheet(0)

// 数据写入

Label label = null

// Title

//label = new jxl.write.Label(3, 0, "到期付款信息表", wcfFC0)

//sheet.addCell(label)

/*

* label = new jxl.write.Label(0, 3, "备注", wcfFC4)

* sheet.addCell(label)

*/

label = new jxl.write.Label(0, 0, "到期日", wcfFC8)

sheet.addCell(label)

label = new jxl.write.Label(1, 0, "部门名称", wcfFC8)

sheet.addCell(label)

label = new jxl.write.Label(2, 0, "币种", wcfFC8)

sheet.addCell(label)

label = new jxl.write.Label(3, 0, "银行", wcfFC8)

sheet.addCell(label)

label = new jxl.write.Label(4, 0, "金额", wcfFC8)

sheet.addCell(label)

label = new jxl.write.Label(5, 0, "业务类型", wcfFC8)

sheet.addCell(label)

label = new jxl.write.Label(6, 0, "摘要", wcfFC8)

sheet.addCell(label)

sheet.setName("到期付款信息查询")

int j = 1

for (int i = 0i <list.size()i++) {

DNCM02 dto = (DNCM02) list.get(i)

label = new Label(0, i + j, CommonUtil.dateToStr(dto.getEndDay()),

wcfFC1)

sheet.addCell(label)

label = new Label(1, i + j, dto.getEndDeptName(), wcfFC1)

sheet.addCell(label)

label = new Label(2, i + j, dto.getCurrencyName(), wcfFC1)

sheet.addCell(label)

label = new Label(3, i + j, dto.getEndBankName(), wcfFC1)

sheet.addCell(label)

label = new Label(4, i + j, CommonUtil.formatString(dto

.getEndAmount().toString()), wcfFC1)

sheet.addCell(label)

label = new Label(5, i + j, dto.getEndTypeName(), wcfFC1)

sheet.addCell(label)

label = new Label(6, i + j, dto.getRemark(), wcfFC1)

sheet.addCell(label)

}

book.write()

book.close()

os.close()

}

然后指定excel的保存路径即可。

页面需做的

<form id='form1' name='form1' action='upload.action' method='post' enctype="multipart/form-data">

<input type='file' id='uploadfile' name = 'uploadfile' />

</form>

action中

属性 public File uploadfile(加get set 方法)

方法 public String upload(){

读文件,解析,对uploadfile进行 *** 作

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11782699.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存