poi操作oracle数据库导出excel文件

HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象
HSSFSheet sheet = workBook.createSheet("数据库表结构");// 创建一个工作薄对象
sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为
/*HSSFRow row = sheet.createRow(1);// 创建一个行对象
row.setHeightInPoints(23);// 设置行高23像素 */

//ResultSet resultSet= DBOperator();
//ResultSet resultSet= DBOperatorModule();
List<Map<String, Object>> list= DBOperatorModule();
Iterator<Map<String, Object>> terIterator = list.iterator();
//设置行数
int rowNum = 0;
while (terIterator.hasNext()) {
Map<String, Object> map = terIterator.next();
//创建标题
HSSFRow row = sheet.createRow(rowNum++);// 创建一个行对象
row.setHeightInPoints(23);// 设置行高23像素
HSSFCellStyle style = workBook.createCellStyle();// 创建样式对象
// 设置字体
HSSFFont font = workBook.createFont();// 创建字体对象
font.setFontHeightInPoints((short) 11);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体
font.setFontName("宋体");// 设置为宋体字
style.setFont(font);// 将字体加入到样式对象
HSSFCell cell = row.createCell(0);// 创建单元格,开始第一个单元格,单元格编号默认从0开始
//可以加黑
//设置每个模块的 模块名称和类型的id
cell.setCellValue(map.get("MODULE_NAME")+"("+map.get("MODULE_TYPE_ID")+")");
cell.setCellStyle(style);
//cell.setCellValue(resultSet.getNString("MODULE_NAME")+"("+resultSet.getNString("MODULE_SHORT_NAME")+")");
//创建元素名称,属性,代码
HSSFRow row1 = sheet.createRow(rowNum++);// 创建一个行对象
row1.setHeightInPoints(23);// 设置行高23像素
HSSFCell cell1 = row1.createCell(1);// 创建单元格
cell1.setCellValue("元素名称");
cell1.setCellStyle(style);
//可以补充设置单元格的宽度。。。。cell1.setCellStyle(style);
HSSFCell cell2 = row1.createCell(2);// 创建单元格
cell2.setCellValue("属性");
cell2.setCellStyle(style);
HSSFCell cell3 = row1.createCell(3);// 创建单元格
cell3.setCellValue("代码");
cell3.setCellStyle(style);

//开始创建元素名称,属性,代码
//元素名称
//select * from PUB_ELEMENT where MIDULE_ID;
String string = pub_elementSql;
String string1 = "MODULE_CODE";
String string2 = (String) map.get("MODULE_CODE");
//获取元素集合
//ResultSet resultSet1= DBOperator(string,string1,string2);
//ResultSet resultSet1= DBOperatorElement(string1,string2);
List<Map<String, Object>> list1= DBOperatorElement(string2);
Iterator<Map<String, Object>> terIterator1 = list1.iterator();

//设置行数
//int rowNum = 3;
//设置列数
//int columnNum = 1;
//设置模块中的相应的元素,写到excel中去
while (terIterator1.hasNext()) {
//设置列数
int columnNum = 1;
Map<String, Object> map1= terIterator1.next();
// 创建一个行对象
HSSFRow rowElement = sheet.createRow(rowNum++);
rowElement.setHeightInPoints(23);// 设置行高23像素
//属性
//select * from PUB_ATTRIBUTE where ELEMENT_ID;
//获取元素名称
String stringElementName = map1.get("ELEMENT_NAME") + "("
+ map1.get("ELEMENT_VAR_NAME") + ")";
HSSFCell cellElment = rowElement.createCell(columnNum);
//添加新行中的第一个单元格数值
cellElment.setCellValue(stringElementName);
columnNum+=1;
//属性名称
String stringAttributeName=null;
//属性名称
String stringCodeName=null;
//查询的表明,字段,id
String string11 = pub_attributeSql;
String string22 = "ELEMENT_CODE";
String string33 = (String) map1.get("ELEMENT_CODE");
//获取属性集合
//ResultSet resultSet2= DBOperator(string11,string22,string33);
//ResultSet resultSet2= DBOperatorAttribute(string22,string33);
List<Map<String, Object>> list2= DBOperatorAttribute(string33);
Iterator<Map<String, Object>> terIterator2 = list2.iterator();
//设置属性,把属性写到Excel中
//用于判断属性个数,多于一个就要添加新行
int yesOrNoAddRow = 0;
//记录新添加行时,需要添加的单元格的位置
int attributeCellInRowNum = 0;
while (terIterator2.hasNext()) {
Map<String, Object> map2= terIterator2.next();
//这里要考虑到表格的合并......,例如患者姓名的 录入值,昵称 就要分两行
//代码
//select * from PUB_ATTRIBUTE where ELEMENT_ID;
//获取属性名称
stringAttributeName=map2.get("ATTR_NAME")+"("+map2.get("ATTR_VAR_NAME")+")";
if(yesOrNoAddRow>0){
// 创建一个行对象
HSSFRow rowElementNew = sheet.createRow(rowNum++);
rowElement.setHeightInPoints(23);// 设置行高23像素
columnNum=attributeCellInRowNum;
HSSFCell cellElment1 = rowElementNew.createCell(attributeCellInRowNum);
//是否添加新行
//attributeCellInRowNum=columnNum;
//添加新行中的第二个单元格数值
cellElment1.setCellValue(stringAttributeName);
columnNum+=1;

String string333 = (String) map2.get("ATTR_CODE");

List<Map<String, Object>> list3= DBOperatorAttCode(string333);
Iterator<Map<String, Object>> terIterator3 = list3.iterator();
while (terIterator3.hasNext()) {
Map<String, Object> map3 = terIterator3.next();
//查询的表明,字段,id
stringCodeName =map3.get("CODE_VALUE")+"."+ map3.get("CODE_NAME");
HSSFCell cellElment2 = rowElementNew.createCell(columnNum);
//添加新行中的第三个单元格数值
cellElment2.setCellValue(stringCodeName);
columnNum+=1;
}
}else {
HSSFCell cellElment1 = rowElement.createCell(columnNum);
//记录新添加行时,需要添加的单元格的位置
attributeCellInRowNum =columnNum;
//添加新行中的第二个单元格数值
cellElment1.setCellValue(stringAttributeName);
columnNum+=1;

String string111 = pub_attr_codeSql;
String string222 = "ATTR_CODE";
String string333 = (String) map2.get("ATTR_CODE");

//ResultSet resultSet3= DBOperator(string111,string222,string333);
//ResultSet resultSet3= DBOperatorAttCode(string222,string333);
List<Map<String, Object>> list3= DBOperatorAttCode(string333);
Iterator<Map<String, Object>> terIterator3 = list3.iterator();
while (terIterator3.hasNext()) {
Map<String, Object> map3 = terIterator3.next();
//查询的表明,字段,id
stringCodeName =map3.get("CODE_VALUE")+"."+ map3.get("CODE_NAME");
HSSFCell cellElment2 = rowElement.createCell(columnNum);
//添加新行中的第三个单元格数值
cellElment2.setCellValue(stringCodeName);
columnNum+=1;
}
}
yesOrNoAddRow+=1;
}
rowNum+=1;
}

HSSFRow row11 = sheet.createRow(rowNum);// 创建一个行对象
row11.setHeightInPoints(23);
HSSFRow row22 = sheet.createRow(rowNum+1);// 创建一个行对象
row22.setHeightInPoints(23);
//cell.setCellStyle(style);// 应用样式对象
/*for (Cell cell4 : row12) {
//循环 创建具体元素名称 属性,代码值的单元格具体值。
}*/
}

FileOutputStream os = new FileOutputStream("style_2003.xls");
workBook.write(os);// 将文档对象写入文件输出流
os.close();// 关闭文件输出流
workBook.close();

System.out.println("创建成功 office 2003 excel");


//creatExcel(resultSet,"人口统计学(DM)");

//creat2003Excel();// 创建2007版Excel文件
//creat2007Excel();// 创建2003版Excel文件
//读取2003Excel文件

/*String path2003 = System.getProperty("user.dir")
+ System.getProperty("file.separator")
+ "style_2003.xls";
// 获取项目文件路径+2003版文件名
System.out.println("路径:" + path2003);
File f2003 = new File(path2003);
try {
readExcel(f2003);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//读取2007Excel文件
String path2007 = System.getProperty("user.dir")
+ System.getProperty("file.separator")
+ "style_2007.xlsx";// 获取项目文件路径+2007版文件名
System.out.println("路径:" + path2007);
File f2007 = new File(path2007);
try {
readExcel(f2007);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/

原文地址:https://www.cnblogs.com/1175429393wljblog/p/5773406.html