您的位置:首页 > 数据库 > Oracle

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

2016-08-16 13:38 579 查看
package com.test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.text.DecimalFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.Date;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import org.apache.poi.hssf.record.DBCellRecord;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;

import org.apache.poi.hssf.usermodel.HSSFComment;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFPatriarch;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import org.apache.poi.xssf.usermodel.XSSFFont;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class testExcel {

public static final String pub_moduleSql = "PUB_MODULE";

public static final String pub_elementSql = "PUB_ELEMENT";

public static final String pub_attributeSql = "PUB_ATTRIBUTE";

public static final String pub_attr_codeSql = "PUB_ATTR_CODE";

/**

* 可以从http://poi.apache.org/ 这里下载到POI的jar包 POI

* 创建和读取2003-2007版本Excel文件

*

*/

@SuppressWarnings("unused")

public static void main(String[] args) throws Exception {

multiSheets();

singleSheet();

/*HSSFWorkbook workBook = new HSSFWorkbook(); // 创建 一个excel文档对象

HSSFSheet sheet =null;

HSSFSheet sheet = workBook.createSheet("数据库表结构");// 创建一个工作薄对象

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(2, 8000);// 设置第三列的宽度为

sheet.setColumnWidth(3, 1000);// 设置第四列的宽度为

sheet.setColumnWidth(4, 1000);// 设置第五列的宽度为

sheet.setColumnWidth(5, 1000);// 设置第六列的宽度为

sheet.setColumnWidth(6, 1000);// 设置第七列的宽度为

sheet.setColumnWidth(7, 1000);// 设置第八列的宽度为

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();

for (int i = 0; i < list.size(); i++) {

sheet = workBook.createSheet();// 创建一个工作薄对象

workBook.setSheetName(i, String.valueOf(i));

}

//设置行数

//int rowNum = 0;

int intAdd = 0;

while (terIterator.hasNext()) {

int rowNum = 0;

Map<String, Object> map = terIterator.next();

String stringMap =map.get("MODULE_NAME") +"(" + map.get("MODULE_TYPE_ID")+")";

//sheet = workBook.getSheetAt(intAdd);// 创建一个工作薄对象

sheet = workBook.createSheet(stringMap);

//workBook.setSheetName(intAdd, stringMap);

intAdd++;

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(2, 10000);// 设置第三列的宽度为

sheet.setColumnWidth(3, 10000);// 设置第四列的宽度为

sheet.setColumnWidth(4, 10000);// 设置第五列的宽度为

sheet.setColumnWidth(5, 10000);// 设置第六列的宽度为

sheet.setColumnWidth(6, 10000);// 设置第七列的宽度为

sheet.setColumnWidth(7, 10000);// 设置第八列的宽度为

//创建标题

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);

//新添加数据类型。。。。

HSSFCell cell3 = row1.createCell(3);// 创建单元格

cell3.setCellValue("数据类型");

cell3.setCellStyle(style);

HSSFCell cell4 = row1.createCell(4);// 创建单元格

cell4.setCellValue("代码");

cell4.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")+") " +map2.get("DATA_TYPE_NAME");

if(yesOrNoAddRow>0){

// 创建一个行对象

HSSFRow rowElementNew = sheet.createRow(rowNum++);

rowElementNew.setHeightInPoints(23);// 设置行高23像素

columnNum=attributeCellInRowNum;

HSSFCell cellElment1 = rowElementNew.createCell(attributeCellInRowNum);

//是否添加新行

//attributeCellInRowNum=columnNum;

//添加新行中的第二个单元格数值

cellElment1.setCellValue(stringAttributeName);

columnNum+=1;

//新添加数据类型列。。。。

HSSFCell cellElmentData = rowElementNew.createCell(columnNum);

String stringAttributeName1=(String) map2.get("DATA_TYPE_NAME");

cellElmentData.setCellValue(stringAttributeName1);

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;

//新添加数据类型列。。。。

HSSFCell cellElmentData = rowElement.createCell(columnNum);

String stringAttributeName1=(String) map2.get("DATA_TYPE_NAME");

cellElmentData.setCellValue(stringAttributeName1);

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();

} */

}

/**

* 创建2003版本的Excel文件 单个sheet

*/

private static void singleSheet() throws Exception {

HSSFWorkbook workBook = new HSSFWorkbook(); // 创建 一个excel文档对象

HSSFSheet sheet = workBook.createSheet("数据库表结构");// 创建一个工作薄对象

//HSSFRow row = sheet.createRow(0);// 创建一个行对象

//row.setHeightInPoints(23);// 设置行高23像素

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(2, 10000);// 设置第三列的宽度为

sheet.setColumnWidth(3, 10000);// 设置第四列的宽度为

sheet.setColumnWidth(4, 10000);// 设置第五列的宽度为

sheet.setColumnWidth(5, 10000);// 设置第六列的宽度为

sheet.setColumnWidth(6, 10000);// 设置第七列的宽度为

sheet.setColumnWidth(7, 10000);// 设置第八列的宽度为

HSSFCellStyle style = workBook.createCellStyle();// 创建样式对象

// 设置字体

HSSFFont font = workBook.createFont();// 创建字体对象

font.setFontHeightInPoints((short) 11);// 设置字体大小

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体

font.setFontName("宋体");// 设置为宋体字

style.setFont(font);// 将字体加入到样式对象

//HSSFRow row = sheet.createRow(0);// 创建一个行对象

//row.setHeightInPoints(23);// 设置行高23像素

//HSSFCell cell = row.createCell(0);// 创建单元格,开始第一个单元格,单元格编号默认从0开始

//可以加黑

//设置每个模块的 模块名称和类型的id

//cell.setCellValue(map.get("MODULE_NAME")+"("+map.get("MODULE_TYPE_ID")+")");

//cell.setCellStyle(style);

//创建元素名称,属性,代码

HSSFRow row1 = sheet.createRow(0);// 创建一个行对象

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);*/

//新添加数据类型。。。。

HSSFCell cell3 = row1.createCell(3);// 创建单元格

cell3.setCellValue("数据类型");

cell3.setCellStyle(style);

HSSFCell cell4 = row1.createCell(4);// 创建单元格

cell4.setCellValue("代码");

cell4.setCellStyle(style);

List<Map<String, Object>> list= DBOperatorModule();

Iterator<Map<String, Object>> terIterator = list.iterator();

//设置行数

int rowNum = 1;//rowNum 放到这里,是设置单个sheet。。。。。。

//int intAdd = 0;

while (terIterator.hasNext()) {

/*

* //int rowNum = 0;//rowNum 放到这里,是设置多个sheet。。。。。。

*

Map<String, Object> map = terIterator.next();

String stringMap =map.get("MODULE_NAME") +"(" + map.get("MODULE_TYPE_ID")+")";

sheet = workBook.createSheet(stringMap);

//workBook.setSheetName(intAdd, stringMap);

*/

Map<String, Object> map = terIterator.next();

//intAdd++;

/*sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(2, 10000);// 设置第三列的宽度为

sheet.setColumnWidth(3, 10000);// 设置第四列的宽度为

sheet.setColumnWidth(4, 10000);// 设置第五列的宽度为

sheet.setColumnWidth(5, 10000);// 设置第六列的宽度为

sheet.setColumnWidth(6, 10000);// 设置第七列的宽度为

sheet.setColumnWidth(7, 10000);// 设置第八列的宽度为

*/
//创建标题

//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);// 将字体加入到样式对象

*/

HSSFRow row = sheet.createRow(rowNum++);// 创建一个行对象

row.setHeightInPoints(23);// 设置行高23像素

HSSFCell cell = row.createCell(0);// 创建单元格,开始第一个单元格,单元格编号默认从0开始

//可以加黑

//设置每个模块的 模块名称和类型的id

cell.setCellValue(map.get("MODULE_NAME")+"("+map.get("MODULE_TYPE_ID")+")");

cell.setCellStyle(style);

/*

//创建元素名称,属性,代码

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);

//新添加数据类型。。。。

HSSFCell cell3 = row1.createCell(3);// 创建单元格

cell3.setCellValue("数据类型");

cell3.setCellStyle(style);

HSSFCell cell4 = row1.createCell(4);// 创建单元格

cell4.setCellValue("代码");

cell4.setCellStyle(style);*/

//开始创建元素名称,属性,代码

//元素名称

//select * from PUB_ELEMENT where MIDULE_ID;

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();

//设置模块中的相应的元素,写到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 string33 = (String) map1.get("ELEMENT_CODE");

//获取属性集合

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;

//新添加数据类型列。。。。

HSSFCell cellElmentData = rowElementNew.createCell(columnNum);

String stringAttributeName1=(String) map2.get("DATA_TYPE_NAME");

cellElmentData.setCellValue(stringAttributeName1);

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;

//新添加数据类型列。。。。

HSSFCell cellElmentData = rowElement.createCell(columnNum);

String stringAttributeName1=(String) map2.get("DATA_TYPE_NAME");

cellElmentData.setCellValue(stringAttributeName1);

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 = 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);

}

FileOutputStream os = null;

try {

os = new FileOutputStream("singlesheetstyle_2003.xls");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

workBook.write(os);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}// 将文档对象写入文件输出流

try {

os.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}// 关闭文件输出流

try {

workBook.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

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();

}*/

}

/**

* 创建2003版本的Excel文件 多个sheet

*/

private static void multiSheets() {

HSSFWorkbook workBook = new HSSFWorkbook(); // 创建 一个excel文档对象

HSSFSheet sheet =null;

/*HSSFSheet sheet = workBook.createSheet("数据库表结构");// 创建一个工作薄对象

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(2, 8000);// 设置第三列的宽度为

sheet.setColumnWidth(3, 1000);// 设置第四列的宽度为

sheet.setColumnWidth(4, 1000);// 设置第五列的宽度为

sheet.setColumnWidth(5, 1000);// 设置第六列的宽度为

sheet.setColumnWidth(6, 1000);// 设置第七列的宽度为

sheet.setColumnWidth(7, 1000);// 设置第八列的宽度为

*/

/*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();

/*for (int i = 0; i < list.size(); i++) {

sheet = workBook.createSheet();// 创建一个工作薄对象

workBook.setSheetName(i, String.valueOf(i));

}*/

//设置行数

//int rowNum = 0;

int intAdd = 0;

while (terIterator.hasNext()) {

int rowNum = 0;

Map<String, Object> map = terIterator.next();

String stringRepMap = map.get("MODULE_NAME") +"(" + map.get("MODULE_TYPE_ID")+")";

stringRepMap= stringRepMap.replace('/', '_');

String stringMap = stringRepMap;

//sheet = workBook.getSheetAt(intAdd);// 创建一个工作薄对象

sheet = workBook.createSheet(stringMap);

//workBook.setSheetName(intAdd, stringMap);

intAdd++;

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

sheet.setColumnWidth(2, 10000);// 设置第三列的宽度为

sheet.setColumnWidth(3, 10000);// 设置第四列的宽度为

sheet.setColumnWidth(4, 10000);// 设置第五列的宽度为

sheet.setColumnWidth(5, 10000);// 设置第六列的宽度为

sheet.setColumnWidth(6, 10000);// 设置第七列的宽度为

sheet.setColumnWidth(7, 10000);// 设置第八列的宽度为

//创建标题

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);*/

//新添加数据类型。。。。

HSSFCell cell3 = row1.createCell(3);// 创建单元格

cell3.setCellValue("数据类型");

cell3.setCellStyle(style);

HSSFCell cell4 = row1.createCell(4);// 创建单元格

cell4.setCellValue("代码");

cell4.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++);

rowElementNew.setHeightInPoints(23);// 设置行高23像素

columnNum=attributeCellInRowNum;

HSSFCell cellElment1 = rowElementNew.createCell(attributeCellInRowNum);

//是否添加新行

//attributeCellInRowNum=columnNum;

//添加新行中的第二个单元格数值

cellElment1.setCellValue(stringAttributeName);

columnNum+=1;

//新添加数据类型列。。。。

HSSFCell cellElmentData = rowElementNew.createCell(columnNum);

String stringAttributeName1=(String) map2.get("DATA_TYPE_NAME");

cellElmentData.setCellValue(stringAttributeName1);

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;

//新添加数据类型列。。。。

HSSFCell cellElmentData = rowElement.createCell(columnNum);

String stringAttributeName1=(String) map2.get("DATA_TYPE_NAME");

cellElmentData.setCellValue(stringAttributeName1);

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 = null;

try {

os = new FileOutputStream("multisheetstyle_2003.xls");

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

workBook.write(os);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}// 将文档对象写入文件输出流

//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();

}*/

}

/**

* 创建版本的Excel文件

*/

private static void creatExcel(ResultSet resultSet ,String titleString) throws FileNotFoundException,IOException {

HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象

HSSFSheet sheet = workBook.createSheet(titleString);// 创建一个工作薄对象

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

HSSFRow row = sheet.createRow(1);// 创建一个行对象

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);// 将字体加入到样式对象

// 设置对齐方式

style.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);// 水平居中

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中

// 设置边框

style.setBorderTop(HSSFCellStyle.BORDER_THICK);// 顶部边框粗线

style.setTopBorderColor(HSSFColor.RED.index);// 设置为红色

style.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);// 底部边框双线

style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);// 左边边框

style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);// 右边边框

/*// 声明一个画图的顶级管理器

HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

// 定义注释的大小和位置,详见文档

HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));

// 设置注释内容

comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));

// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.

comment.setAuthor("leno"); */

// 格式化日期

style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

List<String> list = new ArrayList<String>() ;

int columnIndex =1;

try {

while (resultSet.next()) {

list.add(resultSet.getNString("MODULE_NAME"));

//(columnIndex++));

}

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

int xt=0;

// 创建数据单元格

for (String string : list) {

HSSFCell cell = row.createCell(xt);// 创建单元格

//cell.setCellValue(new Date());// 写入当前日期

cell.setCellValue(string);// 写入当前日期

cell.setCellStyle(style);// 应用样式对象

xt++;

}

// 文件输出流

FileOutputStream os = new FileOutputStream("style_2003.xls");

workBook.write(os);// 将文档对象写入文件输出流

os.close();// 关闭文件输出流

workBook.close();

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

}

/**

* 创建2007版Excel文件

*

* @throws FileNotFoundException

* @throws IOException

*/

private static void creat2007Excel() throws FileNotFoundException,

IOException {

// HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象

XSSFWorkbook workBook = new XSSFWorkbook();

XSSFSheet sheet = workBook.createSheet();// 创建一个工作薄对象

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

XSSFRow row = sheet.createRow(1);// 创建一个行对象

row.setHeightInPoints(23);// 设置行高23像素

XSSFCellStyle style = workBook.createCellStyle();// 创建样式对象

// 设置字体

XSSFFont font = workBook.createFont();// 创建字体对象

font.setFontHeightInPoints((short) 15);// 设置字体大小

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体

font.setFontName("黑体");// 设置为黑体字

style.setFont(font);// 将字体加入到样式对象

// 设置对齐方式

style.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);// 水平居中

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中

// 设置边框

style.setBorderTop(HSSFCellStyle.BORDER_THICK);// 顶部边框粗线

style.setTopBorderColor(HSSFColor.RED.index);// 设置为红色

style.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);// 底部边框双线

style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);// 左边边框

style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);// 右边边框

// 格式化日期

style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

XSSFCell cell = row.createCell(1);// 创建单元格

cell.setCellValue(new Date());// 写入当前日期

cell.setCellStyle(style);// 应用样式对象

// 文件输出流

FileOutputStream os = new FileOutputStream("style_2007.xlsx");

workBook.write(os);// 将文档对象写入文件输出流

os.close();// 关闭文件输出流

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

}

/**

* 创建2003版本的Excel文件

*/

private static void creat2003Excel() throws FileNotFoundException,

IOException {

HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象

HSSFSheet sheet = workBook.createSheet();// 创建一个工作薄对象

sheet.setColumnWidth(1, 10000);// 设置第二列的宽度为

HSSFRow row = sheet.createRow(1);// 创建一个行对象

row.setHeightInPoints(23);// 设置行高23像素

HSSFCellStyle style = workBook.createCellStyle();// 创建样式对象

// 设置字体

HSSFFont font = workBook.createFont();// 创建字体对象

font.setFontHeightInPoints((short) 15);// 设置字体大小

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体

font.setFontName("黑体");// 设置为黑体字

style.setFont(font);// 将字体加入到样式对象

// 设置对齐方式

style.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);// 水平居中

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中

// 设置边框

style.setBorderTop(HSSFCellStyle.BORDER_THICK);// 顶部边框粗线

style.setTopBorderColor(HSSFColor.RED.index);// 设置为红色

style.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);// 底部边框双线

style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);// 左边边框

style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);// 右边边框

/*// 声明一个画图的顶级管理器

HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

// 定义注释的大小和位置,详见文档

HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));

// 设置注释内容

comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));

// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.

comment.setAuthor("leno"); */

// 格式化日期

style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

HSSFCell cell = row.createCell(1);// 创建单元格

cell.setCellValue(new Date());// 写入当前日期

cell.setCellStyle(style);// 应用样式对象

// 文件输出流

FileOutputStream os = new FileOutputStream("style_2003.xls");

workBook.write(os);// 将文档对象写入文件输出流

os.close();// 关闭文件输出流

workBook.close();

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

}

/**

* 对外提供读取excel 的方法

*/

public static List<List<Object>> readExcel(File file) throws IOException {

String fileName = file.getName();

String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName

.substring(fileName.lastIndexOf(".") + 1);

if ("xls".equals(extension)) {

return read2003Excel(file);

} else if ("xlsx".equals(extension)) {

return read2007Excel(file);

} else {

throw new IOException("不支持的文件类型");

}

}

/**

* 读取 office 2003 excel

*

* @throws IOException

* @throws FileNotFoundException

*/

private static List<List<Object>> read2003Excel(File file)

throws IOException {

List<List<Object>> list = new LinkedList<List<Object>>();

HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));

HSSFSheet sheet = hwb.getSheetAt(0);

Object value = null;

HSSFRow row = null;

HSSFCell cell = null;

System.out.println("读取office 2003 excel内容如下:");

for (int i = sheet.getFirstRowNum(); i <= sheet

.getPhysicalNumberOfRows(); i++) {

row = sheet.getRow(i);

if (row == null) {

continue;

}

List<Object> linked = new LinkedList<Object>();

for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {

cell = row.getCell(j);

if (cell == null) {

continue;

}

DecimalFormat df = new DecimalFormat("0");// 格式化 number String

// 字符

SimpleDateFormat sdf = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串

DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字

switch (cell.getCellType()) {

case XSSFCell.CELL_TYPE_STRING:

// System.out.println(i + "行" + j + " 列 is String type");

value = cell.getStringCellValue();

System.out.print(" " + value + " ");

break;

case XSSFCell.CELL_TYPE_NUMERIC:

// System.out.println(i + "行" + j

// + " 列 is Number type ; DateFormt:"

// + cell.getCellStyle().getDataFormatString());

if ("@".equals(cell.getCellStyle().getDataFormatString())) {

value = df.format(cell.getNumericCellValue());

} else if ("General".equals(cell.getCellStyle()

.getDataFormatString())) {

value = nf.format(cell.getNumericCellValue());

} else {

value = sdf.format(HSSFDateUtil.getJavaDate(cell

.getNumericCellValue()));

}

System.out.print(" " + value + " ");

break;

case XSSFCell.CELL_TYPE_BOOLEAN:

// System.out.println(i + "行" + j + " 列 is Boolean type");

value = cell.getBooleanCellValue();

System.out.print(" " + value + " ");

break;

case XSSFCell.CELL_TYPE_BLANK:

// System.out.println(i + "行" + j + " 列 is Blank type");

value = "";

System.out.print(" " + value + " ");

break;

default:

// System.out.println(i + "行" + j + " 列 is default type");

value = cell.toString();

System.out.print(" " + value + " ");

}

if (value == null || "".equals(value)) {

continue;

}

linked.add(value);

}

System.out.println("");

list.add(linked);

}

return list;

}

/**

* 读取Office 2007 excel

*/

private static List<List<Object>> read2007Excel(File file)

throws IOException {

List<List<Object>> list = new LinkedList<List<Object>>();

// String path = System.getProperty("user.dir") +

// System.getProperty("file.separator")+"dd.xlsx";

// System.out.println("路径:"+path);

// 构造 XSSFWorkbook 对象,strPath 传入文件路径

XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));

// 读取第一章表格内容

XSSFSheet sheet = xwb.getSheetAt(0);

Object value = null;

XSSFRow row = null;

XSSFCell cell = null;

System.out.println("读取office 2007 excel内容如下:");

for (int i = sheet.getFirstRowNum(); i <= sheet

.getPhysicalNumberOfRows(); i++) {

row = sheet.getRow(i);

if (row == null) {

continue;

}

List<Object> linked = new LinkedList<Object>();

for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {

cell = row.getCell(j);

if (cell == null) {

continue;

}

DecimalFormat df = new DecimalFormat("0");// 格式化 number String

// 字符

SimpleDateFormat sdf = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串

DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字

switch (cell.getCellType()) {

case XSSFCell.CELL_TYPE_STRING:

// System.out.println(i + "行" + j + " 列 is String type");

value = cell.getStringCellValue();

System.out.print(" " + value + " ");

break;

case XSSFCell.CELL_TYPE_NUMERIC:

// System.out.println(i + "行" + j

// + " 列 is Number type ; DateFormt:"

// + cell.getCellStyle().getDataFormatString());

if ("@".equals(cell.getCellStyle().getDataFormatString())) {

value = df.format(cell.getNumericCellValue());

} else if ("General".equals(cell.getCellStyle()

.getDataFormatString())) {

value = nf.format(cell.getNumericCellValue());

} else {

value = sdf.format(HSSFDateUtil.getJavaDate(cell

.getNumericCellValue()));

}

System.out.print(" " + value + " ");

break;

case XSSFCell.CELL_TYPE_BOOLEAN:

// System.out.println(i + "行" + j + " 列 is Boolean type");

value = cell.getBooleanCellValue();

System.out.print(" " + value + " ");

break;

case XSSFCell.CELL_TYPE_BLANK:

// System.out.println(i + "行" + j + " 列 is Blank type");

value = "";

// System.out.println(value);

break;

default:

// System.out.println(i + "行" + j + " 列 is default type");

value = cell.toString();

System.out.print(" " + value + " ");

}

if (value == null || "".equals(value)) {

continue;

}

linked.add(value);

}

System.out.println("");

list.add(linked);

}

return list;

}

private static ResultSet DBOperator (String... sql) {

java.sql.PreparedStatement preparedStatement = null;

java.sql.ResultSet resultSet = null;

Connection connection = null;

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.250:1521/pdborcl", "pdsci", "123456");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

if (sql.length>1) {

//sql = "select * from ? where ?=?";
;

//preparedStatement= connection.prepareStatement("select * from pub_element where module_code='dc7d576b15b14da1a32bedad7f2874df'");

preparedStatement= connection.prepareStatement("select * from module_code where ?=?");

//设置sql语句中查询表的表名称(即要从哪个表查询)

//preparedStatement.setString(1, sql[0]);

//设置sql语句中的查询表的条件的字段名称

preparedStatement.setString(1, sql[1]);

//设置sql语句中的查询表的的条件的字段名称为指定id

preparedStatement.setString(2, sql[2]);

} else if(sql.length==1) {

//sql = "select * from PUB_ELEMENT where PUB_MODULE_ID=?";

preparedStatement= connection.prepareStatement("select * from ? ");

//设置sql语句中查询表的表名称(即要从哪个表查询)

preparedStatement.setString(1, sql[1]);

}else {

//sql = "select * from PUB_ELEMENT where PUB_MODULE_ID=?";

preparedStatement= connection.prepareStatement("select * from PUB_MODULE ");

}

//statement= connection.createStatement();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet = preparedStatement.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return resultSet;

}

private static List<Map<String, Object>> DBOperatorModule () {

List<Map<String, Object>> list = new ArrayList<>();

java.sql.PreparedStatement preparedStatement = null;

java.sql.ResultSet resultSet = null;

Connection connection = null;

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

//connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.250:1521/pdborcl", "pdsci", "123456");

connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.118:1521/orcl", "pdsci", "123456");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

preparedStatement= connection.prepareStatement("select * from pub_module where record_status ='Y' order by module_name, module_type_id");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet = preparedStatement.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

list=resultSetToList(resultSet);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

preparedStatement.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

connection.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return list;

}

private static List<Map<String, Object>> DBOperatorElement (String... sql) {

List<Map<String, Object>> list = new ArrayList<>();

java.sql.PreparedStatement preparedStatement = null;

java.sql.ResultSet resultSet = null;

Connection connection = null;

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

//connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.250:1521/pdborcl", "pdsci", "123456");

connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.118:1521/orcl", "pdsci", "123456");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

//sql = "select * from ? where ?=?";
;

//preparedStatement= connection.prepareStatement("select * from pub_element where module_code='dc7d576b15b14da1a32bedad7f2874df'");

preparedStatement= connection.prepareStatement("select * from pub_element where module_code=? order by element_name");

//设置sql语句中查询表的表名称(即要从哪个表查询)

//preparedStatement.setString(1, sql[0]);

//设置sql语句中的查询表的条件的字段名称

preparedStatement.setString(1, sql[0]);

//设置sql语句中的查询表的的条件的字段名称为指定id

//preparedStatement.setString(2, sql[1]);

//statement= connection.createStatement();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet = preparedStatement.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet = preparedStatement.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

list=resultSetToList(resultSet);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

preparedStatement.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

connection.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return list;

}

private static List<Map<String, Object>> DBOperatorAttribute (String... sql) {

List<Map<String, Object>> list = new ArrayList<>();

java.sql.PreparedStatement preparedStatement = null;

java.sql.ResultSet resultSet = null;

Connection connection = null;

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

//connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.250:1521/pdborcl", "pdsci", "123456");

connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.118:1521/orcl", "pdsci", "123456");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

//sql = "select * from ? where ?=?";
;

//preparedStatement= connection.prepareStatement("select * from pub_element where module_code='dc7d576b15b14da1a32bedad7f2874df'");

preparedStatement= connection.prepareStatement("select * from pub_attribute where element_code=? order by attr_name");

//设置sql语句中查询表的表名称(即要从哪个表查询)

//preparedStatement.setString(1, sql[0]);

//设置sql语句中的查询表的条件的字段名称

preparedStatement.setString(1, sql[0]);

//设置sql语句中的查询表的的条件的字段名称为指定id

//preparedStatement.setString(2, sql[1]);

//statement= connection.createStatement();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet = preparedStatement.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet = preparedStatement.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

list=resultSetToList(resultSet);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

preparedStatement.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

connection.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return list;

}

private static List<Map<String, Object>> DBOperatorAttCode (String... sql) {

List<Map<String, Object>> list = new ArrayList<>();

java.sql.PreparedStatement preparedStatement = null;

java.sql.ResultSet resultSet = null;

Connection connection = null;

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

//connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.250:1521/pdborcl", "pdsci", "123456");

connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.118:1521/orcl", "pdsci", "123456");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

//sql = "select * from ? where ?=?";

//preparedStatement= connection.prepareStatement("select * from pub_element where module_code='dc7d576b15b14da1a32bedad7f2874df'");

preparedStatement= connection.prepareStatement("select * from pub_attr_code where attr_code=? order by code_value,code_name");

//设置sql语句中查询表的表名称(即要从哪个表查询)

//preparedStatement.setString(1, sql[0]);

//设置sql语句中的查询表的条件的字段名称

preparedStatement.setString(1, sql[0]);

//设置sql语句中的查询表的的条件的字段名称为指定id

//preparedStatement.setString(2, sql[1]);

//statement= connection.createStatement();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet = preparedStatement.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

list=resultSetToList(resultSet);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

preparedStatement.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

connection.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return list;

}

public static List<Map<String, Object>> resultSetToList(ResultSet rs) throws java.sql.SQLException {

if (rs == null)

return null;

ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等

int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

//Map<String, Object> rowData = new HashMap<String, Object>();

while (rs.next()) {

Map<String, Object> rowData = new HashMap<String, Object>();

//rowData = new HashMap<String, Object>(columnCount);

for (int i = 1; i <= columnCount; i++) {

rowData.put(md.getColumnName(i), rs.getObject(i));

}

list.add(rowData);

//System.out.println("list:" + list.toString());

}

System.out.println("list:" + list.toString());

return list;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA poi excel