zoukankan      html  css  js  c++  java
  • 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);
            }  
      }
    }
    

      

  • 相关阅读:
    JQ实现选项卡(jQuery原型插件扩展)
    jQuery的实现原理和核心
    fetch 关于七牛的content-type 的问题
    npm 安装远程包(github的)
    关于dvajs里effects的call和put
    babel6 的 export default bug
    regeneratorRuntime未定义
    babel 解构赋值无法问题
    npm 安装本地包
    datable-默认参数列表
  • 原文地址:https://www.cnblogs.com/Aaronqcd/p/4514477.html
Copyright © 2011-2022 走看看