我想利用jxl导出list到excel,网上的例子list都有实体类,但是我这个list是查几个表以后得到的。。
我想问下 这个应该怎么导呢。。
[code="java"] List> list;//
CellFormat format;// 自己定义
WritableSheet ws;// 电子表格
int rowIndex = 0;
int columnIndex = 0;
for (Map row : list) {
// row写入到excel;
columnIndex = 0;
Set>cellSet=row.entrySet();
for (Entry data : cellSet) {
Label label = new Label(columnIndex, rowIndex, data.getValue(), new WritableCellFormat(format));
try {
ws.addCell(label);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
columnIndex++;
}
rowIndex++;
}[/code]
跟实体类没什么关系,你自己迭代list,一行一行的,每个单元格的把数据写入到excel不就可以了。
[code="java"]public class ExportExcelProjectInfos extends ActionSupport {
private static String projectLevel;
private static String projectSort;
public static String getProjectLevel() {
return projectLevel;
}
public static void setProjectLevel(String projectLevel) {
ExportExcelProjectInfos.projectLevel = projectLevel;
}
public static String getProjectSort() {
return projectSort;
}
public static void setProjectSort(String projectSort) {
ExportExcelProjectInfos.projectSort = projectSort;
}
// 表头
public static final String[] tableHeader = { "竞赛编号", "竞赛名称", "竞赛申请单位", "申报数量限制",
"竞赛类别", "竞赛级别", "开始时间", "结束时间","评分规则编号","申请理由","预申请状态" };
// 创建工作本
public static HSSFWorkbook demoWorkBook = new HSSFWorkbook();
// 创建表
public static HSSFSheet demoSheet = demoWorkBook.createSheet("预申请项目信息");
// 表头的单元格个数目
public static final short cellNumber = (short) tableHeader.length;
// 数据库表的列数
public static final int columNumber = 11;
/**
* 创建表头
*
* @return
*/
public static void createTableHeader() {
HSSFHeader header = demoSheet.getHeader();
header.setCenter("预申请项目信息");
HSSFRow headerRow = demoSheet.createRow((short) 0);
for (int i = 0; i < cellNumber; i++) {
HSSFCell headerCell = headerRow.createCell((short) i);
headerCell.setCellValue(tableHeader[i]);
}
}
/**
* 创建行
*
* @param cells
* @param rowIndex
*/
public static void createTableRow(List<String> cells, short rowIndex) {
// 创建第rowIndex行
HSSFRow row = demoSheet.createRow((short) rowIndex);
for (short i = 0; i < cells.size(); i++) {
// 创建第i个单元格
HSSFCell cell = row.createCell((short) i);
cell.setCellValue(cells.get(i));
}
}
/**
* 创建整个Excel表
*
* @throws SQLException
* @throws UnsupportedEncodingException
*
*/
public static void createExcelSheeet() throws SQLException,
UnsupportedEncodingException {
projectLevel = new String(projectLevel.getBytes("iso8859-1"), "utf-8");
projectSort = new String(projectSort.getBytes("iso8859-1"), "utf-8");
createTableHeader();
System.out.println("projectSort+projectLevel"+projectSort+projectLevel);
ResultSet rs = ExportExcelProjectInfosDAO.getExportInfo(
projectSort, projectLevel);
int rowIndex = 1;
while (rs.next()) {
List<String> list = new ArrayList<String>();
for (int i = 1; i <= columNumber-1; i++) {
list.add(rs.getString(i));
}
if(rs.getString(columNumber).equals("1")){
list.add("通过");
}else if(rs.getString(columNumber).equals("0")){
list.add("未审批");
}else{
list.add("不通过");
}
createTableRow(list, (short) rowIndex);
rowIndex++;
}
}
public InputStream getExportExcel() throws SQLException,
UnsupportedEncodingException {
createExcelSheeet();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
demoWorkBook.write(baos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
[/code]
这个写了好久了,你看看对你有没有帮助。
我用的是poi,大致功能也是将查询结果放到Excel中。
[code="java"]
int n=list.size();
for(int i=0;i<n;i++)
{
Object row=list.get(i);
//写入到excel;
}
[/code]
如果可以用泛型:
[code="java"]
List list;//你的list内部结构是什么
CellFormat format;//自己定义
WritableSheet ws;//电子表格
int rowIndex=0;
int columnIndex=0;
for(String[] row:list){
//row写入到excel;
columnIndex=0;
for(String data:row){
Label=new Label(columnIndex, rowIndex, data, new WritableCellFormat(format));
try {
ws.addCell(label);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
columnIndex++;
}
rowIndex++;
}
[/code]