jsp页面添加数据

jsp页面添加数据,第1张

按你的思路是这样的,首先通过url访问你的jsp页面地址,页面显示出来后用户在表单中填写一些数据并按提交按钮,表单会把数据提交到一个servlet中(就是一个servlet地址),servlet接收到数据后调用dao保存数据,然后把返回的结果放到request中,然后RequestDispatcher dispatcher = request.getRequestDispatcher("你需要返回的jsp页面路径")dispatcher .forward(request, response)就可以了。

主要用poi.jar 包。包含两个jar就可以了:poi-3.16.jar、poi-ooxml-3.16.jar

主要方法分三步:

/**

* filePath 文件路径

* unCaseRow  要排除的行数(从上往下)

* unCaseLine  要排除的列数(从左往右)

*/

public List<String[]> readExcel(String filePath, int unCaseRow, int unCaseLine) throws Exception {

   Sheet sheet = null

    FileInputStream inStream = null

try {

inStream = new FileInputStream(new File(filePath))

Workbook workBook = WorkbookFactory.create(inStream)

sheet = workBook.getSheetAt(0)

} catch (Exception e) {

e.printStackTrace()

throw new Exception()

} finally {

try {

if (inStream != null) {

inStream.close()

}

} catch (IOException e) {

e.printStackTrace()

}

}

List<String[]> list = init(sheet, unCaseRow, unCaseLine)// 需要排除行数

return list

}

// 初始化表格中的每一行,并得到每一个单元格的值

private List<String[]> init(Sheet sheet, int unCaseRow, int unCaseLine) {

int rowNum = sheet.getLastRowNum() + 1 // 从零开始

List<String[]> result = new ArrayList<String[]>()

String[] rowArr = null

Row row = null

Cell cell = null

int rowLength = 0

int rowIndex = 0

String rowStr = null

for (int i = unCaseRow i < rowNum i++) {

row = sheet.getRow(i)

// 每有新的一行,创建一个新的LinkedList对象

rowLength = row.getLastCellNum()

rowIndex = 0

rowArr = new String[LINECOUNT]

for (int j = unCaseLine j < rowLength j++) {

cell = row.getCell(j)

// 获取单元格的值

rowStr = getCellValue(cell)

// 将得到的值放入链表中

rowArr[rowIndex++] = rowStr

}

result.add(rowArr)

}

return result

}

// 获取单元格的值

@SuppressWarnings("deprecation")

private String getCellValue(Cell cell) {

String cellValue = ""

DataFormatter formatter = new DataFormatter()

if (cell != null) {

// 判断单元格数据的类型,不同类型调用不同的方法

switch (cell.getCellType()) {

// 数值类型

case Cell.CELL_TYPE_NUMERIC:

// 进一步判断 ,单元格格式是日期格式

if (DateUtil.isCellDateFormatted(cell)) {

cellValue = formatter.formatCellValue(cell)

} else {

// 数值

double value = cell.getNumericCellValue()

int intValue = (int) value

cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value)

}

break

case Cell.CELL_TYPE_STRING:

cellValue = cell.getStringCellValue()

break

case Cell.CELL_TYPE_BOOLEAN:

cellValue = String.valueOf(cell.getBooleanCellValue())

break

// 判断单元格是公式格式,需要做一种特殊处理来得到相应的值

case Cell.CELL_TYPE_FORMULA: {

try {

cellValue = String.valueOf(cell.getNumericCellValue())

} catch (IllegalStateException e) {

cellValue = String.valueOf(cell.getRichStringCellValue())

}

}

break

case Cell.CELL_TYPE_BLANK:

cellValue = ""

break

case Cell.CELL_TYPE_ERROR:

cellValue = ""

break

default:

cellValue = cell.toString().trim()

break

}

}

return cellValue.trim()

}

解析成对象以后,不论是插入数据库,还是jsp,都是一样的。

插入数据库:hibernate、mybatis

在jsp显示:对象封装进list,在页面显示list。


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

原文地址:https://54852.com/bake/11419442.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存