JXL 写excel文件时string和nubmer类型混乱,网上都搜不到解决办法WritableSheet sheet = wb.createSheet(sheeetName, this.currentSheetIndex);
Label label = new Label(columnIndex, lineIndex, "001111");
label.addCellFeatures();
sheet.addCell(label);label 加入sheet中, "00111"在excel只能看到111,好象是被认为数字型的了,查了很多资料都找不到解决的办法,不知道大家有没有碰到过这个问题

解决方案 »

  1.   

    用Label(int c, int r, String cont, CellFormat st)的这个构造方法试下。可以对写入内容进行格式化。
      

  2.   


    查了很多资料,还是没有找到实现 CellFormat的 text类型的类, 谁有这样的例子,可以给一个吗
      

  3.   

    这个是从jxl的官方demo中提取出来的
    import java.io.File;
    import java.io.IOException;import jxl.CellView;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.format.Colour;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;public class TEST {
        private static void writeLabelsSheet(WritableSheet ws) throws WriteException {
    ws.getSettings().setProtected(true);
    ws.getSettings().setPassword("jxl");
    ws.getSettings().setVerticalFreeze(5);
    ws.getSettings().setDefaultRowHeight(25 * 20);
    WritableFont wf = new WritableFont(WritableFont.ARIAL, 12);
    wf.setItalic(true);
    WritableCellFormat wcf = new WritableCellFormat(wf);
    CellView cv = new CellView();
    cv.setSize(25 * 256);
    cv.setFormat(wcf);
    ws.setColumnView(0, cv);
    ws.setColumnView(1, 15);
    for (int i = 0; i < 10; i++) {
        Label l1 = new Label(0, i, "00455");
        Label l2 = new Label(1, i, "Distinct label number " + i);
        ws.addCell(l1);
        ws.addCell(l2);
    }
    Label l3 = new Label(0, 10, "Common Label", wcf);
    Label l4 = new Label(1, 10, "1-1234567890", wcf);
    Label l5 = new Label(2, 10, "2-1234567890", wcf);
    ws.addCell(l3);
    ws.addCell(l4);
    ws.addCell(l5);
    wf = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD);
    wf.setColour(Colour.RED);
    WritableCellFormat wcf2 = new WritableCellFormat(wf);
    wcf2.setWrap(true);
    Label l = new Label(0, 12, "Different format", wcf2);
    ws.addCell(l);
    Label l6 = new Label(5, 2, "A column for autosizing", wcf2);
    ws.addCell(l6);
    l6 = new Label(5, 4, "Another label, longer this time and in a different font");
    ws.addCell(l6);
    CellView cf = new CellView();
    cf.setAutosize(true);
    ws.setColumnView(5, cf);
        }    public static void main(String[] args) {
    WorkbookSettings ws = new WorkbookSettings();
    WritableWorkbook workbook;
    try {
        workbook = Workbook.createWorkbook(new File("t.xls"), ws);
        WritableSheet s = workbook.createSheet("Labels", 0);
        writeLabelsSheet(s);
        workbook.write();
        workbook.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (WriteException e) {
        e.printStackTrace();
    }
        }
    }
      

  4.   

    jxl没用过,一直用的poi,不过我想他们解决的方式应该是类似的,
    一个是在label构造的时候,指定为处理文字型的构造器
    一个是构造好了以后再设定单元格属性,比如类似于
    label.setCellStyle(CellStyle.STRING); 楼主可以找一下,相信一定有的
      

  5.   

    感谢楼上,加了
    WritableFont wf = new WritableFont(WritableFont.ARIAL, 12);
    wf.setItalic(true);
    WritableCellFormat wcf = new WritableCellFormat(wf); 
    Label label = new Label(columnIndex, lineIndex, "001111", wcf);
    后,输出在excel的内容显示是没有问题了,但是当我在excel中编辑CELL ("001111") TO ("00222"), 离开ell后,就又变成了"222"
    所以上面的方法也不是完美的!
      

  6.   


    感谢这位朋友的宝贵意见,中午我试了下POI,如你所说,是可以的,而且没有我说的第二个问题(在excel中编辑CELL ("001111") TO ("00222"), 离开ell后,就又变成了"222")
    因为对程序中出现问题很执着,所以要是有朋友有JXL更正确的解决方法请贡献出来与大家分享一下,我不是偷懒,自己和几个同事已经一起帮忙找了很久,所以你要是碰到过,那就赶快...:)
      

  7.   

    你把 wcf 改成下面这个 WritableCellFormat wcf = new WritableCellFormat(NumberFormats.TEXT);