问题描述
每天数据都有图片但是导出大量的数据时,图片位置就会串,而且如果我某一条数据没有图片,那么图片就会上传一行,请问要怎么解决呢? 请各位帮忙!package Tool;import java.io.File;import java.io.OutputStream;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import jxl.Workbook;import jxl.format.Colour;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableImage;public class LedgerDataPort {// 导出数据public void export(OutputStream os,Connection con)throws Exception {Statement stmt = con.createStatement(); Statement smt = con.createStatement();ResultSet rs = null;try {String sql;sql = "select id as '序列', photo as '图片',produces as '生产厂家',nuclideName as '核素名',outActivite as '出厂活度',outDate as '日期',classes as '类别',code as '国家编码',status as '状态',saveAdd as '保存地址', remark as '备注',ratify as '审批状态'"+ "from LedgerMessage where ratify ='1'";rs = stmt.executeQuery(sql);// 用JXL向新建的文件中添加内容jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);jxl.write.WritableSheet ws = wwb.createSheet("台帐信息表", 0);WritableFont wfobj=new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD); WritableCellFormat cfobj=new WritableCellFormat(wfobj); cfobj.setBackground(Colour.PINK); cfobj.setWrap(true);int i = 0;////// int j = 0;jxl.write.Label labelC = new jxl.write.Label(0, 0, "序列");Label lblImage=new Label(1,0,"Image", cfobj);ws.addCell(lblImage); WritableImage imgobj=new WritableImage(0, 0, 3, 5, new File("D:/sunset.png"));ws.addImage(imgobj);ws.addCell(labelC);for (int k = 1; k < rs.getMetaData().getColumnCount(); k++) {labelC = new jxl.write.Label(k, 0, rs.getMetaData().getColumnName(k + 1));ws.addCell(labelC);}while (rs.next()) {labelC = new jxl.write.Label(0, i + 1, i + 1 + ""); // 为序列赋值ws.addCell(labelC);for (int k = 1; k < rs.getMetaData().getColumnCount(); k++) {//System.out.println("rs.getString(k + 1) --> "+rs.getString(k + 1) + " k --> "+k);labelC = new jxl.write.Label(k, i + 1, rs.getString(k + 1));ws.addCell(labelC);}ResultSet rst = smt.executeQuery("select * from LedgerMessage where ratify ='1'");rst.close();i++;}wwb.write();wwb.close();} catch (Exception e) {System.out.print(e);e.printStackTrace();} finally {rs.close();smt.close();stmt.close();con.close();}}}
解决方案
当某一行数据没有图片的时候,只是放置图片的额单元格,数据排版不是预想的结果,当没有该条记录没有图片,你进行判断,'图片'这个字段没有数据,你就addCell(new Label(col,ros,""));添加一个空格,站位,防止数据乱了。
解决方案二:
没有数据你也得设计一行啊 不能留着不设置啊 不然行数肯定不对啊。