java excel 搜索_Java连接excel实现:通过姓名查找id和通过id查找姓名

importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.CellType;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;public classDnsTest {public static void main(String[] args) throwsIOException{

getIdbyname("株洲");

getNamebyid(6) ;

}public static void getIdbyname(String name)throwsIOException {//通过name获得id

String filePath="D://dns.xls";

InputStream input= newFileInputStream(filePath);

Workbook wb= null;

wb= newHSSFWorkbook(input);//得到一个工作表对象;

Sheet sheet = wb.getSheetAt(0);int rsRows = sheet.getLastRowNum();//获取sheet表中的总行数//遍历行

for (int i=0;i<=rsRows;i++) {

System.out.println("遍历行数"+i);

Row row=sheet.getRow(i);int id=0;

String flag=null;//遍历行单元格,已知有两列;第一列int型id,第二列String型name

Cell cell1 = row.getCell(0);

Cell cell2= row.getCell(1);if(cell1==null||cell1.equals(null)||cell1.getCellType()==CellType.BLANK){

System.out.println("id为空");break;

}else{//数值型

id=(int) cell1.getNumericCellValue();

};if(cell2==null||cell2.equals(null)||cell2.getCellType()==CellType.BLANK){

System.out.println("name为空");break;

}else{//字符串型

flag=cell2.getStringCellValue();

};

String a=newString(flag);

String b=newString(name);if(a.equals(b)){

System.out.println(id);

};

}

wb.close();//记得关闭

}public static void getNamebyid(int id) throwsIOException {//通过id获得name

String filePath="D://dns.xls";

InputStream input= newFileInputStream(filePath);

Workbook wb= null;

wb= newHSSFWorkbook(input);//得到一个工作表对象;

Sheet sheet = wb.getSheetAt(0);int rsRows = sheet.getLastRowNum();//获取sheet表中的总行数//遍历行

for (int i=0;i<=rsRows;i++) {int flag=0;

String name=null;

Row row=sheet.getRow(i);//遍历行单元格

Cell cell1= row.getCell(0);

Cell cell2= row.getCell(1);if(cell1==null||cell1.equals(null)||cell1.getCellType()==CellType.BLANK){break;

}else{//数值型

flag=(int) cell1.getNumericCellValue();

}if(cell2==null||cell2.equals(null)||cell2.getCellType()==CellType.BLANK){break;

}else{//字符串型

name=cell2.getStringCellValue();

}if(flag==id){

System.out.println(name);

}

}

wb.close();//记得关闭

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个Java读取blob生成excel的示例代码: ```java public void exportExcel() { // 连接数据库并查询需要的blob数据 Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseName", "username", "password"); pstmt = conn.prepareStatement("SELECT excel_data FROM table_name WHERE id = ?"); pstmt.setInt(1, 1); // 假设需要查询的记录ID为1 rs = pstmt.executeQuery(); if (rs.next()) { // 将blob数据转换为excel文件 InputStream inputStream = rs.getBinaryStream("excel_data"); Workbook workbook = WorkbookFactory.create(inputStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); byte[] bytes = outputStream.toByteArray(); // 将生成的excel文件输出到客户端 HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=example.xlsx"); ServletOutputStream out = response.getOutputStream(); out.write(bytes); out.flush(); out.close(); } } catch (SQLException | IOException | InvalidFormatException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 这个示例代码中,首先连接数据库并查询需要的blob数据,然后将这个blob数据转换为excel文件。具体的方法是:先将blob数据读入到一个InputStream中,然后使用Apache POI的WorkbookFactory类的create方法将这个InputStream转换为Workbook对象,最后将这个Workbook对象写入到一个ByteArrayOutputStream中,即可得到生成的excel文件的字节数组。 最后,将这个字节数组输出到客户端即可完成excel文件的下载。具体的方法是:设置response的ContentType为application/vnd.ms-excel,设置response的Header为Content-Disposition:attachment;filename=example.xlsx,然后获取response的OutputStream并将生成的excel文件的字节数组写入到这个OutputStream中,最后关闭OutputStream即可。 希望这个示例代码能够对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值