• salesforce上上传和导出.csv格式文件


    1.导出.csv格式文件:其中会遇到中文乱码问题,会直接生成book.csv文件

    <apex:page controller="BookController" cache="true" contentType="text/csv#book.csv" language="en-US">
        Name
        <apex:repeat value="{!BookWithIndex}" var="a">
            {!a.book.name}
        </apex:repeat>
    </apex:page>
    

      BookController如下:

    public class BookController {
    
        public ApexPages.StandardSetController setCon {
            get {
                if(setCon == null) {
                    setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT name FROM Book1__c]));
                    //setCon.setPageSize(5);
                }
                return setCon;
            }
            set;
        }
        public List<Book1__c> getBooks() {
            return (List<Book1__c>) setCon.getRecords();
        }
        public List<BookWrapper> getBookWithIndex() {
            List<Book1__c> bookList = this.getBooks();
            List<BookWrapper> bookWrapped = new List<BookWrapper>();
            Integer idex = 1;
            for (Book1__c book : bookList) {
                bookWrapped.add(new BookWrapper(book, idex));
                idex++;
            }
            return bookWrapped;
        }
        public class BookWrapper {
            public Book1__c book { get; set; }
            public Integer tabIndex { get; set; }
            public BookWrapper(Book1__c book, Integer tabIndex) {
                this.book = book;
                this.tabIndex = tabIndex;
            }
        }
    }
    

    2.上传.csv格式文件,代码如下:

    <apex:page controller="importDataFromCSVController">
        <apex:form >
            <apex:pagemessages />
            <apex:pageBlock >
                <apex:pageBlockSection columns="4"> 
                      <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                      <apex:commandButton value="Import Book" action="{!importCSVFile}"/>
                </apex:pageBlockSection>
            </apex:pageBlock>
            <apex:pageBlock >
               <apex:pageblocktable value="{!accList}" var="acc">
                  <apex:column value="{!acc.name}" />
            </apex:pageblocktable>
         </apex:pageBlock>
       </apex:form>
    </apex:page>
    

      importDataFromCSVController代码如下:

    public class importDataFromCSVController {
    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    public List<Book1__c> acclist{get;set;}
      public importDataFromCSVController(){
        csvFileLines = new String[]{};
        acclist = New List<Book1__c>(); 
      }
      
      public void importCSVFile(){
           try{
               csvAsString = csvFileBody.toString();
               csvFileLines = csvAsString.split('
    '); 
                
               for(Integer i=1;i<csvFileLines.size();i++){
                   Book1__c accObj = new Book1__c() ;
                   string[] csvRecordData = csvFileLines[i].split(',');
                   accObj.name = csvRecordData[0] ;                                                                                        
                   acclist.add(accObj);   
               }
               insert acclist;
            }
            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);
            }  
      }
    }
    

      

  • 相关阅读:
    接口测试框架开发(三):maven+restAssured+Excel(jxl)+testng+extentreports的接口自动化
    Android下pm 命令详解
    android系统权限SET_PREFERRED_APPLICATIONS怎么获取
    Android 中各种权限深入体验及详解
    Android logcat详细用法
    make_ext4fs 失败
    ubuntu android 设备识别 Setting up a Device for Development
    Unpacking and repacking stock rom .img files
    如何做rom,体验做rom过程,附图文教程,感谢各位romer
    轻松六步教会你如何修改system.img.ext4文件
  • 原文地址:https://www.cnblogs.com/Aaronqcd/p/4514477.html
Copyright © 2020-2023  润新知