如题,运行java程序,excel 文件 总是只插入最后一行代码, 前面都是空的不明原因,请各位高手指教啊~~~~~~~~~public class Date {

public boolean getDate() throws RowsExceededException, WriteException
{
boolean result=false; List liststu=new ArrayList();
// 找到导入的文件
InputStream is= Date.class.getClassLoader().getResourceAsStream("materials.xls");
//创建小字体:Arial,大小为8号,非粗体,非斜体
WritableFont wf = new WritableFont(WritableFont.ARIAL, 8,WritableFont.NO_BOLD, false);
//字体颜色为红色
OutputStream os = null;
WritableWorkbook wwb = null;
String outFileStr = "c:\\test.xls";
try {
//创建工作簿
Workbook wb=Workbook.getWorkbook(is);
//创建工作表
jxl.Sheet sheet=wb.getSheet(0);
// String content=null; 

for(int i=1;i<sheet.getRows();i++)//直接从第二行开始查找
{
ConnCOnnection conn=new ConnCOnnection();
Connection con=conn.getConnect();//建立数据库连接
Student stu=new Student();File file = new File(outFileStr);
if(!file.isFile())//如果指定文件不存在,则新建该文件
file.createNewFile();
os = new FileOutputStream(file);//创建一个输出流
wwb = Workbook.createWorkbook(os);
WritableSheet sheetw = wwb.createSheet("sheet1", 0);//创建一个工作页,第一个参数的页名,第二个参数表示该工作页在excel中处于哪一页for(int j=0;j<sheet.getColumns();j++)//获取列数
{
if (stu.getKind()==null)
{
stu.setKind(sheet.getCell(j, i).getContents());
continue;
}
if(stu.getName()==null)//判断是否有值了
{
stu.setName(sheet.getCell(j, i).getContents());//给name取值为某单元格内容
continue;

}
if(stu.getIn_num()==0)
{
stu.setIn_num(Integer.parseInt(sheet.getCell(j, i).getContents()));
// stu.setIn_num(sheet.getCell(j, i).getContents());
continue;

}
if (stu.getBz()==null) {
stu.setBz(sheet.getCell(j, i).getContents());
continue;
}
if (stu.getInvoice()==null) {
stu.setInvoice(sheet.getCell(j, i).getContents());
continue;
}
if (stu.getIn_price()==0) {
stu.setIn_price(Float.valueOf(sheet.getCell(j, i).getContents()));
continue;
}
if (stu.getProvider()==null) {
stu.setProvider(sheet.getCell(j, i).getContents());
continue;
}
}//内循环结束try {
//sql 数据处理……………………………………
result=true;

WritableCellFormat wcf = new WritableCellFormat(wf);
NumberFormat nf = new NumberFormat("#.###");//格式化数字
wcf = new WritableCellFormat(nf);
Label label = new Label(0,i,stu.getKind());//填充第i行第一列
sheetw.addCell(label);
label = new Label(1,i,stu.getName());
sheetw.addCell(label);
Number nb = new Number(2,i,stu.getIn_num(),wcf);
sheetw.addCell(nb);
label=new Label(3,i,stu.getBz());
sheetw.addCell(label);
label=new Label(4,i,stu.getInvoice());
sheetw.addCell(label);
nb=new Number(5,i,stu.getIn_price(),wcf);
sheetw.addCell(nb);
label=new Label(6,i,stu.getProvider());
sheetw.addCell(label);
WritableFont wfc = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD,false,
UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
WritableCellFormat wcfFC = new WritableCellFormat(wfc); 
wcfFC.setBackground(jxl.format.Colour.RED);//设置单元格的颜色为红色 
label = new jxl.write.Label(7,i,"插入成功!",wcfFC);
sheetw.addCell(label);
wwb.write();//将内容写到excel文件中
con.close();
System.out.println(stu.getKind()+" , " +stu.getName()+"插入成功啦!!");
}catch (Exception e) {
e.printStackTrace();
result= false;
System.out.println(stu.getKind()+" , " +stu.getName()+"插入出错啦!!");
WritableCellFormat wcf = new WritableCellFormat(wf);
NumberFormat nf = new NumberFormat("#.###");//格式化数字
wcf = new WritableCellFormat(nf);
Label label = new Label(0,i,stu.getKind());//填充第一行第一个单元格的内容
sheetw.addCell(label);
label = new Label(1,i,stu.getName());
sheetw.addCell(label);
Number nb = new Number(2,i,stu.getIn_num(),wcf);
sheetw.addCell(nb);
label=new Label(3,i,stu.getBz());
sheetw.addCell(label);
label=new Label(4,i,stu.getInvoice());
sheetw.addCell(label);
nb=new Number(5,i,stu.getIn_price(),wcf);
sheetw.addCell(nb);
//sheetw.addCell(label);
label=new Label(6,i,stu.getProvider());
sheetw.addCell(label);
WritableFont wfc = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD,false,
UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
WritableCellFormat wcfFC = new WritableCellFormat(wfc); 
wcfFC.setBackground(jxl.format.Colour.RED);//设置单元格的颜色为红色 
label = new jxl.write.Label(7,i,"插入出错啦!",wcfFC);
sheetw.addCell(label);
wwb.write();
con.close();
}}//外循环结束
} catch (Exception e) {
e.printStackTrace();

finally{
try {
 if(wwb != null)
 wwb.close();
 if(os != null)
os.flush();
 os.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return result;
} public static void main(String[] args) throws RowsExceededException, WriteException {
Date d=new Date();
d.getDate();
}

}

解决方案 »

  1.   

    os = new FileOutputStream(file);// 创建一个输出流
    wwb = Workbook.createWorkbook(os);你这2行是什么意思,这2行写在了读取目标excel的行的循环里面,意思就是读取目标excel的时候每次都会新创建一个工作表,那么肯定只能保留目标EXCEL里面的最后一行。
      

  2.   

    这2行是什么意思,这2行写在了读取目标excel的行的循环里面,意思就是读取目标excel的时候每次都会新创建一个工作表,那么肯定只能保留目标EXCEL里面的最后一行。 
    循环过早关闭了吧文件
      

  3.   

    File file = new File(outFileStr);
    if(!file.isFile())//如果指定文件不存在,则新建该文件
    file.createNewFile();
    os = new FileOutputStream(file);//创建一个输出流
    wwb = Workbook.createWorkbook(os);
    WritableSheet sheetw = wwb.createSheet("sheet1", 0);但当我将这几行代码 放到循环外面的时候,生成的excel就只有第一行数据,这又是为什么?再次求教!!!!!
      

  4.   

    按上面改好后自己写个main方法单步调试一下,很快就能找出原因了。