1.先貼上VF頁面的代碼經過在<apex:page contenttype="application/x-excel# GenExcel.xls>能夠實現生成Excelhtml
實現Excel的導出VF頁面代碼以下數組
<apex:page controller="BookController" contenttype="application/x-excel# GenExcel.xls" showheader="false"> app
<head>fetch
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />ui
</head>url
<apex:dataTable value="{!acclist}" var="acc" border="1">debug
<apex:column >excel
<apex:facet name="header">Name</apex:facet>code
{!acc.Name}orm
</apex:column>
<apex:column >
<apex:facet name="header">Phone</apex:facet>
{!acc.Phone}
</apex:column>
<apex:column >
<apex:facet name="header">AccountNumber</apex:facet>
{!acc.AccountNumber}
</apex:column>
</apex:dataTable>
</apex:page>
apex類後臺控制器代碼
public class BookController {
public List<Account> acclist{get;set;}
public List<Account> getBookWithIndex() {
String fetchAllGoods = 'SELECT Name,Phone,AccountNumber FROM Account';
system.debug(fetchAllGoods);
acclist = Database.query(fetchAllGoods);
return acclist;
}
}
二 、實現CSV、EXcel的導入都是一樣的方法
VF頁面代碼
<apex:page controller="importDataFromCSVController">
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
<apex:commandButton value="Import Account" action="{!importCSVFile}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock >
<apex:pageblocktable value="{!accList}" var="acc">
<apex:column value="{!acc.Name}" />
<apex:column value="{!acc.Phone}" />
<apex:column value="{!acc.AccountNumber}" />
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex類控制後臺代碼實現
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<Account> acclist{get;set;}
public importDataFromCSVController(){
csvFileLines = new String[]{};
acclist = New List<Account>();
}
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<csvFileLines.size();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);
}
}