现在的位置: 首页 > 综合 > 正文

java对excel文件的读写操作

2013年10月08日 ⁄ 综合 ⁄ 共 11332字 ⁄ 字号 评论关闭
通过Jakarta的POI项目提供了一组操纵Windows文档的Java API 来完成对excel文件操作
该API有如下几个类提供了操作Excel文件的方便的途径:
  1. HSSFWorkbook ==>Excel电子表格中的 Book
  2. HSSFSheet ==>Excel电子表格中的 Sheet
  3. HSSFRow ==>Excel电子表格中的
  4. HSSFCell ==>Excel电子表格中的 单元格
    1. package business;
    2. import org.apache.poi.hssf.usermodel.*;
    3. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.IOException;
    7. /**
    8.  * Jakarta的POI项目提供了一组操纵Windows文档的Java API,如下几个类提供了操作Excel文件的方便的途径:<br>
    9.  * HSSFWorkbook、HSSFSheet、HSSFRow、HSSFCell,<br>
    10.  * 分别代表了Excel电子表格中的Book、Sheet、行和单元格。 <br>
    11.  */
    12. public class CreateExcel {
    13.     public static void main(String[] args) throws Exception {
    14.         write();
    15.         System.out.println(read("d://test.xls"));
    16.     }
    17.     /**
    18.      * 写时:解决中文乱码问题
    19.      * cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    20.      * 
    21.      * 通过 FileOutputStream 来完生成的xls文件的保存
    22.      */
    23.     private static void write() {
    24.         FileOutputStream fos = null;
    25.         HSSFWorkbook wb = new HSSFWorkbook();
    26.         HSSFSheet s = wb.createSheet();
    27.         wb.setSheetName(0, "first sheet");
    28.         
    29.         StringBuffer str = new StringBuffer();
    30.         str.append("#@#Acct GroupAttribute Change,,,,FIN_SRV- Banking,,,,,,,,,Bank Of China,,,Enterprise,,,China ConstructionBank,hailali,CH_S_ENT_NA_FSI_CCBhailali,Enterprise,Local GovernmentRegulations,the current groupname(China Construction Bank)is wrong for this account:BOC Hainan, pls helpto change it to correct group namd:Bank Of China,OK to submit");
    31.         str.append("#@#Acct GroupAttribute Change,,,,FIN_SRV- Banking,,,,,,,,,The People's Bank OfChina,,,Enterprise,,,GOVERNMENT OF CHINA,hailali,CH_S_ENT_NA_FSI_PBOChailali,Enterprise,Local GovernmentRegulations,the current groupname(Government of China )is wrong for this account:PBOC Yulin, pls help tochange it to correct group namd:The People's Bank Of China,OK to submit");
    32.         str.append("#@#Acct GroupAttribute Change,,,,ENG/UTL- NaturalGas/Electric/Water,,,,,,,,,South Power GridCorporation,,,Enterprise,,,Guangxi Power Company,hailali,CH_S_ENT_NA_LENT_POWERhailali,Enterprise,Local GovernmentRegulations,the account should beunder FVEV group name: South Power Grid Corporation,OK to submit");
    33.         str.append("#@#Acct GroupAttribute Change,,,,ENG/UTL- NaturalGas/Electric/Water,,,,,,,,,South Power GridCorporation,,,Enterprise,,,Hainan PowerCorporation Limite,hailali,CH_S_ENT_NA_LENT_POWERhailali,Enterprise,Local GovernmentRegulations,the account should beunder FVEV group name: South Power Grid Corporation,OK to submit");
    34.         str.append("#@#Acct GroupAttribute Change,,,,ENG/UTL- NaturalGas/Electric/Water,,,,,,,,,South Power GridCorporation,,,Enterprise,,,Datang Tanyan WaterPower Generator,hailali,CH_S_ENT_NA_LENT_POWERhailali,Enterprise,Local GovernmentRegulations,the account should beunder FVEV group name: South Power Grid Corporation,OK to submit");
    35.         str.append("#@#Acct GroupAttribute Change,,,,FIN_SRV- Banking,,,,,,,,,The People's Bank OfChina,,,Enterprise,,,China ConstructionBank,haiyxu,CH_S_ENT_NA_FSI_CCBhaiyxu,Enterprise,Local GovernmentRegulations,the account should beunder FVEV group name: The People's Bank Of China,OK to submit");
    36.         str.append("#@#Acct Delete,No,DATANG TANYAN WATERPOWER GENERATOR,,,37156485,0013000000Cziiv,,,,,,,,,,,,,,hailali,CH_S_ENT_NA_LENT_POWERhailali,Enterprise,Merger &Acquisition,this account is mergein Datang Power plant, which is not belongs to hailali's account,OK to submit");
    37.         str.append("#@#Acct Delete,No,Shenzhen Metro Co.,Ltd.,,,2797093,0013000000D2X9w,,,,,,,,,,,,,,yli2,CH_S_ENT_NA_LENT_MORyli2,Enterprise,Duplicate Account,account duplicate innamed account list, pls delete,OK to submit");
    38.         str.append("#@#Acct GroupAttribute Change,,,,ENG/UTL- NaturalGas/Electric/Water,3225272,,,,,,,,Guangdong YudeanGroup Corporation Limited,,,Enterprise,,,Huaneng Power CompanyLimited,yli2,CH_S_ENT_NA_LENT_POWERyli2,Enterprise,Local GovernmentRegulations,the account should beunder FVEV group name: Guangdong Yudean Group Corporation Limited,OK to submit");
    39.         str.append("#@#Acct GroupAttribute Change,,,,ENG/UTL- NaturalGas/Electric/Water,,,,,,,,,Guangdong YudeanGroup Corporation Limited,,,Enterprise,,,Huaneng Haimen PowerPlan,yli2,CH_S_ENT_NA_LENT_POWERyli2,Enterprise,Local GovernmentRegulations,the account should beunder FVEV group name: Guangdong Yudean Group Corporation Limited,OK to submit");
    40.         str.append("#@#Acct Delete,Yes,Shenzhen MindrayCo.,Ltd,深圳迈瑞生物医疗电子股份有限公司,,39421686,0013000000It5ak,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. isduplicate with Mei Wenfeng's acct. Mindray Medical International Limited,which has no chinese name, so make confusion before,OK to submit");
    41.         str.append("#@#Acct Delete,Yes,Zhongcheng BondCo.,Ltd,众成证券经纪有限公司,,89284977,0013000000It5Zb,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. wastransferd to CH_COMM_SZX_ISAM_01 already,OK to submit");
    42.         str.append("#@#Acct Delete,Yes,Guangxi JinGui Pulp& Paper Co., Ltd,,,83297032,0013000000I0cpI,,,,,,,,,,,,,,wuwen,CH_COMM_GX_ISAM_01(CH_S_COMM_GX_TAM_TM),Commercial Mid-MarketTAM,Duplicate Account,Duplicate Account,OK to submit");
    43.         str.append("#@#Acct Delete,Yes,guangxiwomen&children healthy hospital,,,34055092,0013000000IP1E3,,,,,,,,,,,,,,wuwen,CH_COMM_GX_ISAM_01(CH_S_COMM_GX_TAM_TM),Commercial Mid-MarketTAM,Duplicate Account,Duplicate Account,OK to submit");
    44.         str.append("#@#Acct Regroup,,GUANGDONG MIDEAELECTRIC APPLIANCE COMPANY LTD,,,110081399,,,,,,,,MIDEA GROUP,,,,,,GUANGDONG MIDEAELECTRIC APPLIANCE COMPANY LTD,yasu,CH_S_ENT_GD_O_LENT_TMyasu,,Merger &Acquisition,Belong to new group,OK to submit");
    45.         str.append("#@#Acct Delete,Yes,guangxiwomen&children healthy hospital,,,34055092,0013000000IP1E3,,,,,,,,,,,,,,wuwen,CH_COMM_GX_ISAM_01(CH_S_COMM_GX_TAM_TM),Commercial Mid-MarketTAM,Duplicate Account,Duplicate Accountwith ISAM.,OK to submit");
    46.         str.append("#@#Acct Delete,Yes,Shenzhen MindrayCo.,Ltd,深圳迈瑞生物医疗电子股份有限公司,,39421686,0013000000It5ak,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. isduplicate with Mei Wenfeng's acct. Mindray Medical International Limited,which has no chinese name, so make confusion before,OK to submit");
    47.         str.append("#@#Acct Delete,Yes,Zhongcheng BondCo.,Ltd,众成证券经纪有限公司,,89284977,0013000000It5Zb,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. wastransferd to CH_COMM_SZX_ISAM_01 already,OK to submit");
    48.         str.append("#@#Acct Delete,Yes,Trust Mart Shanghai,,,2733443,0013000000GdUaN,,,,,,,,,,,,,,lilzhong,CH_S_ENT_SZ_LENT_TMlilzhong,Enterprise,Duplicate Account,notbelong to,OK to submit");
    49.         str.append("#@#Acct Delete,Yes,guangdong zhong youpu tai mobile communication equipment,,,10282152,0013000000KFumZ,,,,,,,,,,,,,,kahe,CH_S_ENT_GZ_LENT_TMkahe,Enterprise,Duplicate Account,notbelong to,OK to submit");
    50.         str.append("#@#Acct Delete,Yes,Xiamen MunicipalGovernment,,,38333226,0013000000ClAUs,,,,,,,,,,,,,,xigeng,CH_S_ENT_FJ_PS_TMxigeng,Enterprise,Duplicate Account,notbelong to,OK to submit");
    51.         str.append("#@#Acct Delete,Yes,Xiamen MunicipalGovernment,,,42112821,0013000000D1GAJ,,,,,,,,,,,,,,xigeng,CH_S_ENT_FJ_PS_TMxigeng,Enterprise,Duplicate Account,notbelong to,OK to submit");
    52.         str.append("#@#Acct Delete,Yes,Wal-Mart BusinessInformation Corporation Limited,,,2749753,,,,,,,,,,,,,,,lilzhong,CH_S_ENT_SZ_LENT_TMlilzhong,Enterprise,Duplicate Account,notbelong to,FAILED: SFDC Account IDcan't be blank.");
    53.         str.append("#@#Acct Delete,Yes,China CTS Co.,Ltd,,,,,,,,,,,,,,,,,,hodu,CH_S_COMM_SZ_TAM_TMhodu,Commercial Mid-MarketTAM,Duplicate Account,Duplicate Accountwith China National Travel Service Group Corporation, Account ID:0013000000JFNYk.,FAILED:CSC ID can't be blank. SFDC Account ID can't be blank.");
    54.         str.append("#@#Acct Delete,Yes,HASBRO TOYS SHENZHENCOMPANY LTD,孩之宝玩具(深圳)有限公司,,,,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,Thisacct. is duplicate with CH COMM SOUTH SISAM 02's HASBRO TOYS, which has nochinese name too.,FAILED:CSC ID can't be blank. SFDC Account ID can't be blank.");
    55.         str.append("#@#Acct Delete,Yes,Xunlei companyLimited,迅雷 ,,,,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. isduplicate with CH_COMM_SZX_ISAM_03's xunlei,FAILED:CSC ID can't be blank. SFDC Account ID can't be blank.");
    56.         str.append("#@#Acct Delete,Yes,Xunlei companyLimited,迅雷 ,,,,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. isduplicate with CH_COMM_SZX_ISAM_03's xunlei,FAILED:CSC ID can't be blank. SFDC Account ID can't be blank.");
    57.         str.append("#@#Acct Delete,Yes,OOCL corp.,东方海外货柜航运(中国)有限公司深圳分公司,,,,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. isduplicate with the acct.东方海外货柜航运(中国)有限公司 of zeliu of south comm.a,FAILED:CSC ID can't be blank. SFDC Account ID can't be blank.");
    58.         str.append("#@#Acct Delete,Yes,HASBRO TOYS SHENZHENCOMPANY LTD,孩之宝玩具(深圳)有限公司,,,,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,Thisacct. is duplicate with CH COMM SOUTH SISAM 02's HASBRO TOYS, which has nochinese name too.,FAILED:CSC ID can't be blank. SFDC Account ID can't be blank.");
    59.         str.append("#@#Acct Delete,Yes,OOCL corp.,东方海外货柜航运(中国)有限公司深圳分公司,,,,,,,,,,,,,,,,,yiqyu,CH_S_COMM_SZ_TAM_TMyiqyu,Commercial Mid-MarketTAM,Duplicate Account,This acct. isduplicate with the acct.东方海外货柜航运(中国)有限公司 of zeliu of south comm.a,FAILED:CSC ID can't be blank. SFDC Account ID can't be blank.");
    60.         
    61.         String t1 = str.toString();
    62.         String t2[] = t1.split("#@#");
    63.         String result[][] = new String[t2.length][30];
    64.         for(int i = 0 ; i < t2.length ; i ++){
    65.             result[i] = t2[i].split(",");
    66.         }
    67.         for(int i = 0 ; i < result.length ; i++){
    68.             HSSFRow row = s.createRow(i);
    69.             for(int j = 0  ; j < result[i].length ; j++){
    70.                 HSSFCell cell = row.createCell((short) j);
    71.                 cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    72.                 cell.setCellValue(result[i][j]);
    73.             }
    74.         }
    75.         
    76.         
    77.         try {
    78.             fos = new FileOutputStream("d://test.xls");
    79.             wb.write(fos);
    80.             fos.close();
    81.         } catch (IOException e) {
    82.             e.printStackTrace();
    83.         }
    84.     }
    85.     /**
    86.      * 两个有用的属性:<br>
    87.      * int rowsnum = sheet.getPhysicalNumberOfRows(); //返回当前sheetr的有效行数
    88.      * int cellsnum = sheet.getRow(sheet.getFirstRowNum()).getPhysicalNumberOfCells();//返回当前行的有效列数
    89.      * 
    90.      * @param url 读取指定的xls文件(当前可以是:d://test.xls),注意,要把路径中的 / 替换为 // 哦
    91.      * @return 单个字符串(cell),可以是一维的(row),可以是二维的(sheet),可以是三维的(book)呵呵,自己定吧
    92.      */
    93.     private static String read(String url) {
    94.         POIFSFileSystem fs = null;
    95.         HSSFWorkbook wb = null;
    96.         try {
    97.             fs = new POIFSFileSystem(new FileInputStream(url));
    98.             wb = new HSSFWorkbook(fs);
    99.         } catch (IOException e) {
    100.             e.printStackTrace();
    101.         }
    102.         HSSFSheet sheet = wb.getSheetAt(0);
    103.         HSSFRow row = sheet.getRow(0);
    104.         HSSFCell cell = row.getCell((short) 0);
    105.         String msg = cell.getStringCellValue();
    106.         return msg;
    107.     }
    108. }

抱歉!评论已关闭.