VF实现Excel/CSV的导入导出

1.先贴上VF页面的代码通过在实现Excel的导出VF页面代码如下

 

    

  

 

        

            Name

            {!acc.Name}

        

        

            Phone

            {!acc.Phone}

        

        

            AccountNumber

            {!acc.AccountNumber}

        

    

 

 

 

apex类后台控制器代码

public class BookController {

   public List acclist{get;set;}

 

       

   

    public List getBookWithIndex() {

        String fetchAllGoods = 'SELECT Name,Phone,AccountNumber  FROM Account';

        system.debug(fetchAllGoods);

        acclist = Database.query(fetchAllGoods);   

        return acclist;

    }

 

    

}

 

二 、实现CSV、EXcel的导入都是同样的方法

VF页面代码

    

        

        

            

                  

                  

            

        

        

           

              

              

               

        

     

   

 

Apex类控制后台代码实现

 

public class importDataFromCSVController {

    

public Blob csvFileBody{get;set;}

public string csvAsString{get;set;}

public String[] csvFileLines{get;set;}

public List acclist{get;set;}

  public importDataFromCSVController(){

    csvFileLines = new String[]{};

    acclist = New List();

  }

   

  public void importCSVFile(){

      system.debug('ssss');

       try{

           csvAsString = bitToString(csvFileBody,'gb2312');

           system.debug(csvAsString);

           csvFileLines = csvAsString.split('\n');

           system.debug(csvFileLines.size());

           for(Integer i=1;i

               system.debug('Account');

               Account accObj = new Account() ;

               string[] csvRecordData = csvFileLines[i].split(',');

               system.debug(csvRecordData);

               accObj.Name = csvRecordData[0] ;    

               accObj.Phone=csvRecordData[1];

               accObj.AccountNumber=csvRecordData[2];

               acclist.add(accObj);

               system.debug('gg'+ accObj.AccountNumber);

           }

           system.debug(acclist);

           insert acclist;

           ApexPages.Message successMsg = new ApexPages.Message(ApexPages.severity.INFO,'import success');

           ApexPages.addMessage(successMsg);

        }

        catch (Exception e)

        {

            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');

            ApexPages.addMessage(errorMessage);

        }

  }

    

    

     public static String bitToString(Blob input, String inCharset){

        //转换成16进制

         String hex = EncodingUtil.convertToHex(input);

         //一个String类型两个字节 32位(bit),则一个String长度应该为两个16进制的长度,所以此处向右平移一个单位,即除以2

         //向右平移一个单位在正数情况下等同于除以2,负数情况下不等

        //eg 9  00001001  >>1 00000100   结果为49        

         final Integer bytesCount = hex.length() >> 1;      

        // //声明String数组,长度为16进制转换成字符串的长度1         

         String[] bytes = new String[bytesCount];

         for(Integer i = 0; i < bytesCount; ++i) {

             //将相邻两位的16进制字符串放在一个String中

             bytes[i] =  hex.mid(i << 1, 2);

         }

        //解码成指定charset的字符串

         return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);

     }

}

 

 

转载于:https://my.oschina.net/u/3459265/blog/3096767

你可能感兴趣的:(VF实现Excel/CSV的导入导出)