这两天在学习了程序生成excel报表
Java中生成excel主要有两种:POI和Excel API
我用到的是POI。。。。。
以下是我测试的代码:
Java类:
public class PoiTest {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("d:\\foo.xls");
String sql = "select * from fullsegment";
List list = new ArrayList();
list = new ExcelDao().Select(sql);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
wb.setSheetName(0, "Matrix");
for(short i=0; i<list.size(); i++) {
HSSFRow row = s.createRow(i);
for(short j=0; j<6; j++) {
HSSFCell cell = row.createCell(j);
//cell.setCellValue(""+i+","+j);
cell.setCellFormula((String) ((List)list.get(i)).get(j));
//cell.set
}
}
wb.write(fos);
fos.close();
}
}
dao类:
public class ExcelDao {
private Connection conn = null;
public ExcelDao() {
conn = new DBProfile().initDB();
}
public List Select(String sql){
List list = new ArrayList();
List list1 = new ArrayList();
try {
Statement smt = conn.createStatement();
ResultSet rs = smt.executeQuery(sql);
while(rs.next()){/*
int start = rs.getInt("start");
int end = rs.getInt("end");
String area = rs.getString("area");
int areaid = rs.getInt("areaid");
int type = rs.getInt("type");
int cityid= rs.getInt("cityid");*/
list.add(rs.getString("start"));
list.add(rs.getString("end"));
list.add(rs.getString("area"));
list.add(rs.getString("areaid"));
list.add(rs.getString("type"));
list.add(rs.getString("cityid"));
list1.add(list);
}
rs.close();
smt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list1;
}
}
数据库表:
CREATE TABLE `fullsegment` (
`start` bigint(13) unsigned NOT NULL default '0',
`end` bigint(13) unsigned NOT NULL default '0',
`area` varchar(10) character set utf8 NOT NULL default '',
`areaid` smallint(9) NOT NULL default '0',
`type` tinyint(3) NOT NULL default '0',
`cityid` smallint(5) NOT NULL default '0',
UNIQUE KEY `start` (`start`,`end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
以上代码我测试过,成功的话,会在D盘下创建一个foo.xls文件,如果表中记录不为空,
那么excel表中是有记录的。。。。。。
补充:
在查询数据库记录时,是有一个问题的,得到结果集之后取数据,如果是这样取数据:
int start = rs.getInt("start");
int end = rs.getInt("end");
String area = rs.getString("area");
int areaid = rs.getInt("areaid");
int type = rs.getInt("type");
int cityid= rs.getInt("cityid");*/
那么会跑出一个sql异常:。。。is outside valid range for the datatype integer.
因为数据库表中字段的类型是bigint类型的
类型不匹配,就跑出上面的异常。。。。。解决方案未找到,但不会影响程序的运行
因此我用的是这种方式取数据:
list.add(rs.getString("start"));
list.add(rs.getString("end"));
list.add(rs.getString("area"));
list.add(rs.getString("areaid"));
list.add(rs.getString("type"));
list.add(rs.getString("cityid"));
Java中生成excel主要有两种:POI和Excel API
我用到的是POI。。。。。
以下是我测试的代码:
Java类:
public class PoiTest {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("d:\\foo.xls");
String sql = "select * from fullsegment";
List list = new ArrayList();
list = new ExcelDao().Select(sql);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
wb.setSheetName(0, "Matrix");
for(short i=0; i<list.size(); i++) {
HSSFRow row = s.createRow(i);
for(short j=0; j<6; j++) {
HSSFCell cell = row.createCell(j);
//cell.setCellValue(""+i+","+j);
cell.setCellFormula((String) ((List)list.get(i)).get(j));
//cell.set
}
}
wb.write(fos);
fos.close();
}
}
dao类:
public class ExcelDao {
private Connection conn = null;
public ExcelDao() {
conn = new DBProfile().initDB();
}
public List Select(String sql){
List list = new ArrayList();
List list1 = new ArrayList();
try {
Statement smt = conn.createStatement();
ResultSet rs = smt.executeQuery(sql);
while(rs.next()){/*
int start = rs.getInt("start");
int end = rs.getInt("end");
String area = rs.getString("area");
int areaid = rs.getInt("areaid");
int type = rs.getInt("type");
int cityid= rs.getInt("cityid");*/
list.add(rs.getString("start"));
list.add(rs.getString("end"));
list.add(rs.getString("area"));
list.add(rs.getString("areaid"));
list.add(rs.getString("type"));
list.add(rs.getString("cityid"));
list1.add(list);
}
rs.close();
smt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list1;
}
}
数据库表:
CREATE TABLE `fullsegment` (
`start` bigint(13) unsigned NOT NULL default '0',
`end` bigint(13) unsigned NOT NULL default '0',
`area` varchar(10) character set utf8 NOT NULL default '',
`areaid` smallint(9) NOT NULL default '0',
`type` tinyint(3) NOT NULL default '0',
`cityid` smallint(5) NOT NULL default '0',
UNIQUE KEY `start` (`start`,`end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
以上代码我测试过,成功的话,会在D盘下创建一个foo.xls文件,如果表中记录不为空,
那么excel表中是有记录的。。。。。。
补充:
在查询数据库记录时,是有一个问题的,得到结果集之后取数据,如果是这样取数据:
int start = rs.getInt("start");
int end = rs.getInt("end");
String area = rs.getString("area");
int areaid = rs.getInt("areaid");
int type = rs.getInt("type");
int cityid= rs.getInt("cityid");*/
那么会跑出一个sql异常:。。。is outside valid range for the datatype integer.
因为数据库表中字段的类型是bigint类型的
类型不匹配,就跑出上面的异常。。。。。解决方案未找到,但不会影响程序的运行
因此我用的是这种方式取数据:
list.add(rs.getString("start"));
list.add(rs.getString("end"));
list.add(rs.getString("area"));
list.add(rs.getString("areaid"));
list.add(rs.getString("type"));
list.add(rs.getString("cityid"));