导出Excel问题,第一条数据不见了
时间:2011-12-09
来源:互联网
public void exportExcel(String title, Collection<T> dataset,
OutputStream out) {
// 声明一个工作薄
try {
//首先检查数据看是否是正确的
Iterator<T> its = dataset.iterator();
if(dataset==null||!its.hasNext()||title==null||out==null)
{
throw new Exception("传入的数据不对!");
}
//取得实际泛型类
T ts = (T) its.next();
Class tCls = ts.getClass();
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置标题样式
style = ExcelStyle.setHeadStyle(workbook, style);
// 得到所有字段
Field filed[] = ts.getClass().getDeclaredFields();
// 标题
List<String> exportfieldtile = new ArrayList<String>();
// 导出的字段的get方法
List<Method> methodObj = new ArrayList<Method>();
// 遍历整个filed
for (int i = 0; i < filed.length; i++) {
Field f = filed[i];
ExcelAnnotation exa = f.getAnnotation(ExcelAnnotation.class);
// 如果设置了annottion
if (exa != null) {
String exprot = exa.exportName();
// 添加到标题
exportfieldtile.add(exprot);
// 添加到需要导出的字段的方法
String fieldname = f.getName();
String getMethodName = "get"
+ fieldname.substring(0, 1).toUpperCase()
+ fieldname.substring(1);
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
methodObj.add(getMethod);
}
}
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < exportfieldtile.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(
exportfieldtile.get(i));
cell.setCellValue(text);
}
int index = 0;
// 循环整个集合
while (its.hasNext()) {
//从第二行开始写,第一行是标题
index++;
row = sheet.createRow(index);
T t = (T) its.next();
for (int k = 0; k < methodObj.size(); k++) {
HSSFCell cell = row.createCell(k);
Method getMethod=methodObj.get(k);
Object value = getMethod.invoke(t, new Object[] {});
String textValue = getValue(value);
cell.setCellValue(textValue);
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
数据库数据:
1 kk a [email protected] M 2010-10-24 16:30:28
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59
导出数据:
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59
OutputStream out) {
// 声明一个工作薄
try {
//首先检查数据看是否是正确的
Iterator<T> its = dataset.iterator();
if(dataset==null||!its.hasNext()||title==null||out==null)
{
throw new Exception("传入的数据不对!");
}
//取得实际泛型类
T ts = (T) its.next();
Class tCls = ts.getClass();
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置标题样式
style = ExcelStyle.setHeadStyle(workbook, style);
// 得到所有字段
Field filed[] = ts.getClass().getDeclaredFields();
// 标题
List<String> exportfieldtile = new ArrayList<String>();
// 导出的字段的get方法
List<Method> methodObj = new ArrayList<Method>();
// 遍历整个filed
for (int i = 0; i < filed.length; i++) {
Field f = filed[i];
ExcelAnnotation exa = f.getAnnotation(ExcelAnnotation.class);
// 如果设置了annottion
if (exa != null) {
String exprot = exa.exportName();
// 添加到标题
exportfieldtile.add(exprot);
// 添加到需要导出的字段的方法
String fieldname = f.getName();
String getMethodName = "get"
+ fieldname.substring(0, 1).toUpperCase()
+ fieldname.substring(1);
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
methodObj.add(getMethod);
}
}
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < exportfieldtile.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(
exportfieldtile.get(i));
cell.setCellValue(text);
}
int index = 0;
// 循环整个集合
while (its.hasNext()) {
//从第二行开始写,第一行是标题
index++;
row = sheet.createRow(index);
T t = (T) its.next();
for (int k = 0; k < methodObj.size(); k++) {
HSSFCell cell = row.createCell(k);
Method getMethod=methodObj.get(k);
Object value = getMethod.invoke(t, new Object[] {});
String textValue = getValue(value);
cell.setCellValue(textValue);
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
数据库数据:
1 kk a [email protected] M 2010-10-24 16:30:28
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59
导出数据:
2 张三 a [email protected] F 2010-10-24 16:37:33
3 李四 a [email protected] M 2010-10-24 16:52:03
4 王五 a [email protected] F 2010-10-24 16:57:04
5 刘柳 a [email protected] M 2010-10-24 17:00:13
6 张峰 a [email protected] M 2010-10-24 17:03:12
7 王琦 a [email protected] M 2010-10-24 17:08:35
8 等待 a [email protected] F 2010-10-24 17:10:33
9 芦娟 a [email protected] F 2010-10-24 17:14:29
10 法则 a [email protected] F 2010-10-24 17:15:59
作者: ding_tengfei 发布时间: 2011-12-09
高手 高手 跪求呀!!! mysql数据库
作者: ding_tengfei 发布时间: 2011-12-09
你那里注释的不是从第二行开始,这样导致了第一行被标题覆盖,所以他就不出来
作者: batelei 发布时间: 2011-12-09
调试,看从哪里最先开始不见的
作者: dracularking 发布时间: 2011-12-09
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28