`
thtwin
  • 浏览: 161194 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

jxl生成excel

    博客分类:
  • J2SE
阅读更多
package campaign;

import com.sun.rowset.CachedRowSetImpl;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.File;

public class CampOption
...{
               
  static SimpleDateFormat myDateFormat     = new SimpleDateFormat("yyyyMMddHHmmss");  
  static SimpleDateFormat myDateFormat1    = new SimpleDateFormat("yyyy");  
  static SimpleDateFormat myDateFormat2    = new SimpleDateFormat("yyyyMMdd");  
  static java.text.DecimalFormat myformat  = new java.text.DecimalFormat("#0.0%"); 
  static java.text.DecimalFormat myformat2 = new java.text.DecimalFormat("#0");
  static java.text.DecimalFormat myformat3 = new java.text.DecimalFormat("#0.00");   


  public CampOption()
  ...{ }

  //导出某个Object[][]数组的内容到excel=====================================
  public static boolean createExcel(Object[][] myTable,String path)
  ...{     
      //System.out.println("+++"+System.getProperty("user.dir"));    //System.getProperty("user.dir")为当前程序运行目录
      //如果不存在该文件夹,则建立新文件夹
      //String path=""+System.getProperty("user.dir")+"/NCI/NciPlan生成文件";
      //System.out.println("+++"+path);
      //java.io.File dir= new java.io.File(path);
      //if (!dir.exists()) { dir.mkdir(); }
     try
     ...{
       //创建工作表与sheet的引用  
       WritableWorkbook   workbook   =   null; 

       //String outputFile=""+path+"/MyList_"+myDateFormat.format(new java.util.Date())+".xls";
       String outputFile=path;
       String worksheet = "List";
        OutputStream os;
    
               os = new FileOutputStream(outputFile);
               workbook = Workbook.createWorkbook(os);

      //创建第一页的sheet  
      WritableSheet sheet1 = workbook.createSheet("MySheet1", 0); //可添加第一个工作
      //WritableSheet sheet2 = workbook.createSheet("MySheet2", 1); //可添加第二个工作
 
       /**//*//此段为将第一行写入项目字段的代码
       for (int i=0; i<title.length; i++)
       {
          //Label(列号,行号 ,内容 )
          Label label = new jxl.write.Label(i, 0, title[i]); //put the title in row1
          sheet1.addCell(label);
       }
       */
         for (int i=0; i<myTable.length; i++)
          ...{
             for(int j=0; j<myTable[i].length; j++)
             ...{
                //设置值  
                 if(myTable[i][j]==null)
                  ...{myTable[i][j]=" ";}
                Label   label   =   new   Label(j, i, myTable[i][j].toString());   //如果输出字段名则此处i改为i+1 ,否则此处从第0行开始直接写数据
                //加到sheet1上  
                sheet1.addCell(label);
             }
          } 
              //输出到文件  
              workbook.write();  
              //关闭文件  
              workbook.close(); 
            return true;
        }
        catch (FileNotFoundException ex)
        ...{ex.printStackTrace();}
        catch (IOException ex)
        ...{ex.printStackTrace();}
        catch (WriteException ex)
        ...{ex.printStackTrace();}
      return false;
  }




  //导出某个Vector包含Object[]结构的内容到excel==============================================
  public static boolean createExcel(Vector myTable,String path)
  ...{     
      //System.out.println("+++"+System.getProperty("user.dir"));    //System.getProperty("user.dir")为当前程序运行目录
      //如果不存在该文件夹,则建立新文件夹
      //String path=""+System.getProperty("user.dir")+"/NCI/NciPlan生成文件";
      //java.io.File dir= new java.io.File(path);
      //if (!dir.exists()) { dir.mkdir(); }
       try
       ...{
         //创建工作表与sheet的引用  
         WritableWorkbook   workbook   =   null; 

         //String outputFile=""+path+"/MyList_"+myDateFormat.format(new java.util.Date())+".xls";
         String outputFile=path;
         String worksheet = "List";//输出的excel文件工作表名
         //String[] title = {"序号","客户姓名","联系电话","拜访内容","再访时间","递交建议书数量","客户来源","是否签要保书","是否收取保费","金额","件数","犹豫期退保件数"};//excel工作表的标题

             OutputStream os=new FileOutputStream(outputFile);
             workbook = Workbook.createWorkbook(os);

           //创建第一页的sheet  
           WritableSheet sheet1 = workbook.createSheet("MySheet1", 0); //可添加第一个工作
           //WritableSheet sheet2 = workbook.createSheet("MySheet2", 1); //可添加第二个工作
 
          /**//*//此段为将第一行写入项目字段的代码
          for (int i=0; i<title.length; i++)
          {
              //Label(列号,行号 ,内容 )
              Label label = new jxl.write.Label(i, 0, title[i]); //put the title in row1
              sheet1.addCell(label);
          }
         */
            for (int i=0; i<myTable.size(); i++)
            ...{
              Object[] myObj = (Object[]) myTable.get(i);
              for(int j=0; j<myObj.length; j++)
              ...{
                  //设置值  
                  if(myObj[j]==null)
                  ...{myObj[j]=" ";}
                  Label   label   =   new   Label(j, i, myObj[j].toString());   //如果输出字段名则此处i改为i+1 ,否则此处从第0行开始直接写数据
                  //加到sheet1上  
                  sheet1.addCell(label);
              }
            } 
             //输出到文件  
             workbook.write();  
             //关闭文件  
             workbook.close();
             return true;
        }
        catch (FileNotFoundException ex)
        ...{ex.printStackTrace();}
        catch (IOException ex)
        ...{ex.printStackTrace();}
        catch (WriteException ex)
        ...{ex.printStackTrace();}
    return false;
  }

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhengpeiyong/archive/2007/12/31/2006052.aspx
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics