java中如何实现报表导入excel,Java传入Excel表格读取数据-怎么用java将一个excel里面数据读出并写入另一个ex......

用java编写代码按照正确格式读取完整excel表格的数...

// 以下为我在项目中运jxl心代码

try {

Workbook book = null;

book = Workbook.getWorkbook(file);

// 获得第一个工作表对象

Sheet sheet = book.getSheet(0);

int rows = sheet.getRows();

if(rows>1){

Map idnomap = this.getStudentByIdnos(sheet);

for (int j = 1; j 

TStudentinfo stu = this.getStudent(sheet,sheet.getRow(j),rows,j,appmap,classmap,idnomap,state);

if(stu==null){

continue;

}

stulist.add(stu);

}

}

book.close();

// DEMO: db 操作在下方

xxxService.add(stulist);

}

catch (Exception e) {

System.out.println(e);

}

java程序怎么查询excel表格数据

// 以下为我在项目中运jxl心代码

try {

Workbook book = null;

book = Workbook.getWorkbook(file);

// 获得第一个工作表对象

Sheet sheet = book.getSheet(0);

int rows = sheet.getRows();

if(rows>1){

Map idnomap = this.getStudentByIdnos(sheet);

for (int j = 1; j 

TStudentinfo stu = this.getStudent(sheet,sheet.getRow(j),rows,j,appmap,classmap,idnomap,state);

if(stu==null){

continue;

}

stulist.add(stu);

}

}

book.close();

// DEMO: db 操作在下方

xxxService.add(stulist);

}

catch (Exception e) {

System.out.println(e);

}

如何用JAVA将数据库中的数据导入到excel表格?

// 以下为我在项目中运jxl心代码

try {

Workbook book = null;

book = Workbook.getWorkbook(file);

// 获得第一个工作表对象

Sheet sheet = book.getSheet(0);

int rows = sheet.getRows();

if(rows>1){

Map idnomap = this.getStudentByIdnos(sheet);

for (int j = 1; j 

TStudentinfo stu = this.getStudent(sheet,sheet.getRow(j),rows,j,appmap,classmap,idnomap,state);

if(stu==null){

continue;

}

stulist.add(stu);

}

}

book.close();

// DEMO: db 操作在下方

xxxService.add(stulist);

}

catch (Exception e) {

System.out.println(e);

}

从excel表格读取数据用Java代码实现批量上传写入数...

// 以下为我在项目中运jxl心代码

try {

Workbook book = null;

book = Workbook.getWorkbook(file);

// 获得第一个工作表对象

Sheet sheet = book.getSheet(0);

int rows = sheet.getRows();

if(rows>1){

Map idnomap = this.getStudentByIdnos(sheet);

for (int j = 1; j 

TStudentinfo stu = this.getStudent(sheet,sheet.getRow(j),rows,j,appmap,classmap,idnomap,state);

if(stu==null){

continue;

}

stulist.add(stu);

}

}

book.close();

// DEMO: db 操作在下方

xxxService.add(stulist);

}

catch (Exception e) {

System.out.println(e);

}

怎么用java将一个excel里面数据读出并写入另一个ex...

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

主法分三步:

/**

* filePath 文件路径

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

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

*/

public List 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 list = init(sheet, unCaseRow, unCaseLine);// 需要排除行数

return list;

}

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

private List init(Sheet sheet, int unCaseRow, int unCaseLine) {

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

List result = new ArrayList();

String[] rowArr = null;

Row row = null;

Cell cell = null;

int rowLength = 0;

int rowIndex = 0;

String rowStr = null;

for (int i = unCaseRow; i 

row = sheet.getRow(i);

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

rowLength = row.getLastCellNum();

rowIndex = 0;

rowArr = new String[LINECOUNT];

for (int j = unCaseLine; 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。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值