JAVA 导入导出EXCEL文件操作

[java]  view plain  copy
 print ?
  1. package com.base.util;  
  2.   
  3. import java.io.*;  
  4. import jxl.*;  
  5. import jxl.write.*;  
  6. import jxl.write.biff.LabelRecord;  
  7. import jxl.write.biff.RowsExceededException;  
  8. import jxl.format.*;  
  9.   
  10. import java.text.SimpleDateFormat;  
  11. import java.util.*;  
  12. import java.awt.Color;  
  13. import jxl.write.Number;  
  14. import jxl.write.Boolean;  
  15.   
  16.   
  17.   
  18.   
  19.   
  20. import com.cms.bean.RegUser;  
  21.   
  22. public class excelTest {  
  23.       
  24.   
  25.      public static  List list=new ArrayList();     
  26.       
  27.     //输出流,X轴Y轴; 自定义关联表导出  
  28. //  public static void writeExcel(OutputStream os,int x,int y,List content) throws Exception {  
  29. //       RegUser user=new RegUser();  
  30. //       String str="";  
  31. //      jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);  
  32. //      jxl.write.WritableSheet ws = wwb.createSheet("Sheet", 0);  
  33. //        
  34.         jxl.write.Label labelC = new jxl.write.Label(x, y,content);  
  35.         labelC = new jxl.write.Label(4, 4,content);  
  36.           
  37. //      if (ws != null) {  
  38. //  
  39. //          // 下面开始添加单元格  
  40. //          for (int i = 0; i < x; i++) {  
  41. //                
  42. //                
  43. //              for (int j = 0; j < y; j++) {  
  44. //                  // 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行  
  45. //                  user=(RegUser)content.get(i);  
  46. //                    
  47. //                  if(j==0){  
  48. //                      str=user.getUserName();  
  49. //                  }else if(j==1){  
  50. //                      str=user.getPassword();  
  51. //                  }else if(j==2){  
  52. //                      str=user.getUserContent();  
  53. //                  }else if(j==3){  
  54. //                      str=user.getFlag();  
  55. //                  }else if(j==4){  
  56. //                      str=user.getUserTime();  
  57. //                  }else if(j==5){  
  58. //                      str=String.valueOf(user.getId());  
  59. //                  }  
  60. //                    
  61. //                    
  62. //                  jxl.write.Label labelC = new Label(j, i,str);  
  63. //                  try {  
  64. //                      // 将生成的单元格添加到工作表中  
  65. //                      ws.addCell(labelC);  
  66. //                  } catch (RowsExceededException e) {  
  67. //                      e.printStackTrace();  
  68. //                  } catch (WriteException e) {  
  69. //                      e.printStackTrace();  
  70. //                  }  
  71. //  
  72. //              }  
  73. //          }  
  74. //  
  75. //      }  
  76. //  
  77. //  
  78. //  
  79. //      /*  
  80. //       * 自定义单元格内样式;  
  81. //       */  
  82. //      // jxl.write.WritableFont wfc = new  
  83. //      // jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD,  
  84. //      // false,UnderlineStyle.NO_UNDERLINE);  
  85. //      // jxl.write.WritableCellFormat wcfFC = new  
  86. //      // jxl.write.WritableCellFormat(wfc);  
  87. //      //单元格加红  
  88. //      //wcfFC.setBackground(jxl.format.Colour.RED);  
  89. //      //labelC = new jxl.write.Label(6, 0, "中国爱我 ", wcfFC);  
  90. //        
  91. //        
  92. //        
  93. //      //ws.addCell(labelC);  
  94. //      // 写入Exel工作表  
  95. //      wwb.write();  
  96. //      // 关闭Excel工作薄对象  
  97. //      wwb.close();  
  98. //    
  99. //  }  
  100.       
  101.   
  102.       
  103.       
  104.       
  105.       
  106.       
  107.     //new file ()  
  108.       
  109.     /** 
  110.      * 读取Excel 
  111.      * 
  112.      * @param filePath 
  113.      */  
  114.     public static void readExcel(String filePath)  
  115.     {  
  116.          String data2="";  
  117.          //DateTime tem=new DateTime("");  
  118.          SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
  119.         try  
  120.         {  
  121.             InputStream is = new FileInputStream(filePath);  
  122.             Workbook rwb = Workbook.getWorkbook(is);  
  123.             //Sheet st = rwb.getSheet("0")这里有两种方法获取sheet表,1为名字,而为下标,从0开始  
  124.             Sheet st = rwb.getSheet("Sheet1");  
  125.             int rs=st.getColumns();  
  126.             int rows=st.getRows();  
  127.             System.out.println("列数===>"+rs+"行数:"+rows);  
  128.               
  129.              
  130.                 for(int k=0;k<rows;k++){//行  
  131.                      for(int i=0 ;i<rs;i++){//列  
  132.                            
  133.                    Cell c00 = st.getCell(i,k);  
  134.             //通用的获取cell值的方式,返回字符串  
  135.             String strc00 = c00.getContents();  
  136.             //获得cell具体类型值的方式  
  137.             if(c00.getType() == CellType.LABEL)  
  138.             {  
  139.                 LabelCell labelc00 = (LabelCell)c00;  
  140.                 strc00 = labelc00.getString();  
  141.             }  
  142.             //excel 类型为时间类型处理;  
  143.             if(c00.getType()==CellType.DATE){  
  144.                 DateCell dc=(DateCell)c00;    
  145.                 strc00 = sdf.format(dc.getDate());  
  146.                   
  147.             }  
  148.             //excel 类型为数值类型处理;  
  149.             /* 
  150.             if(c00.getType()==CellType.NUMBER|| c00.getType()==CellType.NUMBER_FORMULA){ 
  151.                 NumberCell nc=(NumberCell)c00;  
  152.                 strc00=""+nc.getValue();  
  153.             }*/  
  154.               
  155.             //输出  
  156.             System.out.println(">"+strc00);  
  157.               
  158.             list.add(strc00);  
  159.               
  160.          
  161.          //列,行  
  162. //           data2=String.valueOf(st.getCell(1,k).getContents());  
  163. //           data2=data2.replace("/", "-");  
  164. //           java.util.Date dt=sdf.parse(data2);      
  165. //           System.out.println(sdf.format(dt));     
  166. //                            
  167.                      }  
  168.          System.out.println(data2+"======"+list.get(k)+"=========");       
  169.        }  
  170.               
  171.               
  172.             //关闭  
  173.             rwb.close();  
  174.         }  
  175.         catch(Exception e)  
  176.         {  
  177.             e.printStackTrace();  
  178.         }  
  179.     }  
  180.   
  181.     /** 
  182.      * 输出Excel 
  183.      * 
  184.      * @param os 
  185.      */  
  186.     public static void writeExcel(OutputStream os)  
  187.     {  
  188.         try  
  189.         {  
  190.             /** 
  191.              * 只能通过API提供的工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数, 
  192.              * 因为类WritableWorkbook的构造函数为protected类型 
  193.              * method(1)直接从目标文件中读取WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile)); 
  194.              * method(2)如下实例所示 将WritableWorkbook直接写入到输出流 
  195.  
  196.              */  
  197.             WritableWorkbook wwb = Workbook.createWorkbook(os);  
  198.             //创建Excel工作表 指定名称和位置  
  199.             WritableSheet ws = wwb.createSheet("Test Sheet 1",0);  
  200.   
  201.             //**************往工作表中添加数据*****************  
  202.   
  203.             //1.添加Label对象  
  204.             Label label = new Label(0,0,"this is a label test");  
  205.             ws.addCell(label);  
  206.   
  207.             //添加带有字型Formatting对象  
  208.             WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);  
  209.             WritableCellFormat wcf = new WritableCellFormat(wf);  
  210.             Label labelcf = new Label(1,0,"this is a label test",wcf);  
  211.             ws.addCell(labelcf);  
  212.   
  213.             //添加带有字体颜色的Formatting对象  
  214.             WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,  
  215.                     UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);  
  216.             WritableCellFormat wcfFC = new WritableCellFormat(wfc);  
  217.             Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);  
  218.             ws.addCell(labelCF);  
  219.   
  220.             //2.添加Number对象  
  221.             Number labelN = new Number(0,1,3.1415926);  
  222.             ws.addCell(labelN);  
  223.   
  224.             //添加带有formatting的Number对象  
  225.             NumberFormat nf = new NumberFormat("#.##");  
  226.             WritableCellFormat wcfN = new WritableCellFormat(nf);  
  227.             Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);  
  228.             ws.addCell(labelNF);  
  229.   
  230.             //3.添加Boolean对象  
  231.             Boolean labelB = new jxl.write.Boolean(0,2,false);  
  232.             ws.addCell(labelB);  
  233.   
  234.             //4.添加DateTime对象  
  235.             jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());  
  236.             ws.addCell(labelDT);  
  237.   
  238.             //添加带有formatting的DateFormat对象  
  239.             DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");  
  240.             WritableCellFormat wcfDF = new WritableCellFormat(df);  
  241.             DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);  
  242.             ws.addCell(labelDTF);  
  243.   
  244.   
  245.             //添加图片对象,jxl只支持png格式图片  
  246.             File image = new File("d://2.png");  
  247.             WritableImage wimage = new WritableImage(0,1,2,2,image);  
  248.             ws.addImage(wimage);  
  249.             //写入工作表  
  250.             wwb.write();  
  251.             wwb.close();  
  252.         }  
  253.         catch(Exception e)  
  254.         {  
  255.             e.printStackTrace();  
  256.         }  
  257.     }  
  258.   
  259.        
  260.       
  261.     /**  
  262.      * 拷贝后,进行修改,其中file1为被copy对象,file2为修改后创建的对象  
  263.      * 尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去,  
  264.      * 以使单元格的内容以不同的形式表现  
  265.      * @param file1  
  266.      * @param file2  
  267.      */  
  268.     public static void modifyExcel(File file1,File file2)  
  269.     {  
  270.         try  
  271.         {  
  272.             Workbook rwb = Workbook.getWorkbook(file1);  
  273.             WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//copy  
  274.             WritableSheet ws = wwb.getSheet(0);  
  275.             WritableCell wc = ws.getWritableCell(0,0);  
  276.             //判断单元格的类型,做出相应的转换  
  277.             if(wc.getType() == CellType.LABEL)  
  278.             {  
  279.                 Label label = (Label)wc;  
  280.                 label.setString("The value has been modified");  
  281.             }  
  282.             wwb.write();  
  283.             wwb.close();  
  284.             rwb.close();  
  285.         }  
  286.         catch(Exception e)  
  287.         {  
  288.             e.printStackTrace();  
  289.         }  
  290.     }  
  291.   
  292.   
  293.     //测试  
  294.     public static void main(String[] args)  
  295.     {  
  296.         try  
  297.         {  
  298.             //读Excel  
  299.             excelTest.readExcel("D:/testRead.xls");  
  300.             //输出Excel  
  301.            // File fileWrite = new File("D:/testWrite.xls");  
  302.             //fileWrite.createNewFile();  
  303.             //OutputStream os = new FileOutputStream(fileWrite);  
  304.            // excelTest.writeExcel(os);  
  305.             //修改Excel  
  306.           //  excelTest.modifyExcel(new File(""),new File(""));  
  307.         }  
  308.         catch(Exception e)  
  309.         {  
  310.            e.printStackTrace();  
  311.         }  
  312.     }  
  313.   
  314.       
  315.   
  316. }  

 

 

 

JAVA EXCEL JAR 下载:http://download.csdn.net/source/3205282

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值