求JSP读取Excel表格内容的代码

求JSP读取Excel表格内容的代码,第1张

Resource resource = new ClassPathResource("/applicationContext.xml")

BeanFactory factory = new XmlBeanFactory(resource)

SessionFactory sessionFactory = (SessionFactory) factory

.getBean("sessionFactory")

Session session = sessionFactory.openSession()

File excelfile = new File(ServletActionContext.getRequest()

.getRealPath("/upload")

+ "/" + filepath)

Workbook book = null

int rownum

String mname = null

try {

book = Workbook.getWorkbook(excelfile)

Sheet sheet = book.getSheet(0)// 工作簿是从0开始的

for (rownum = 1rownum <sheet.getRows()rownum++) {

mname = sheet.getCell(0, rownum).getContents().trim()

String sql = "insert into media values (seq_media.nextval,?,?,?,?,?,?)"

Query query = session.createSQLQuery(sql)

query.setString(0, mname)

query.executeUpdate()

session.beginTransaction().commit()

}

} catch (Exception e) {

e.printStackTrace()

} finally {

book.close()

}

这个是我的代码 希望帮到你 采纳我 谢谢

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的保存路径即可。


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

原文地址: https://outofmemory.cn/tougao/8086278.html

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

发表评论

登录后才能评论

评论列表(0条)

保存