将字符串插入Excel工作表时出现问题

时间:2013-12-03 06:21:31

标签: java excel

我正在尝试创建一个可以具有名称和ID值的excel文件   制作bol的行在我的程序中给了我错误。请帮助我,因为可能是错误...请  如果可能的话,还可以帮我解决只能将字符串中的名称写入excel文件的代码。

    package demos;
    import jxl.*;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import java.io.*;
    import java.util.*;
    import com.sun.rowset.internal.Row;
    import jxl.CellView;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.biff.RowsExceededException;

    public class StringInp 
    {

   public static void main(String[] args) throws IOException 
    {
        try
        {
            String filename="C:\\virclipse\\input.xls";
    WritableWorkbook wb=Workbook.createWorkbook(new File(filename));

    //Create a blank sheet
    WritableSheet sheet = wb.createSheet("Employee Data",0);

    //This data needs to be written (Object[])
    Map<String, Object[]> data = new TreeMap<String, Object[]>();
    data.put("1", new Object[] {"ID", "NAME"});
    data.put("2", new Object[] {101, "Shivany"});
    data.put("3", new Object[] {102, "Nalini"});
    data.put("4", new Object[] {103, "John"});
    data.put("5", new Object[] {104, "Ayush"});

    //Iterate over data and write to sheet
    Set<String> keyset = data.keySet();
    int rownum = 0;
    for (String key : keyset)
    {
    ***Row row = sheet.createRow(rownum++);***
    Object [] objArr = data.get(key);
    int cellnum = 0;

    for (Object obj : objArr)
    {
        ***Cell cell = row.createCell(cellnum++);***

        if(obj instanceof String)
        {
     ***cell.setCellValue((String)obj);***
        }
    else if(obj instanceof Integer)
    {
    ***cell.setCellValue((Integer)obj);***
               }
        }
    //Write the workbook in file system
    FileOutputStream out = new FileOutputStream(new File("filename"));
    wb.write();
    wb.close();
          } 
    }
    catch(WriteException e)
        {
        System.out.println("there is an error");
        }
    }
    }

2 个答案:

答案 0 :(得分:0)

您正在使用正确的导入,但您还需要在构建路径中拥有所需的jxml jar。

答案 1 :(得分:0)

你可以read here,这里的jExcel API是tutorial你也可以从here下载API请下载jexcelapi_2_6_12.zip,下载后将其解压缩并将jxl.jar放入你的构建路径,那些错误应该去