Below is sample code to to get all object and their fields in CSV using apex in salesforce. In this sample code we are getting objects using namespace and making CSV file.
pubLIC class ObjectFieldsinReport{
public void makecsvwithobjectandfield(){
map<string, SObjectType> objs = schema.getGlobalDescribe();
string header = 'Object Name, Field, Label \n';
string finalstr = header ;
for(string key: objs.keySet()){
//if condtion to handle namespace objects
//remove this key.startsWithIgnoreCase('Your objects namespace') if you want to use all objects
if(key.startsWithIgnoreCase('Your objects namespace') && key.endsWithIgnoreCase('__c')){
map<string, string> fieldList = new map<string, string>();
if(key != null){
map<string,SObjectField> fList = schema.getGlobalDescribe().get(key).getDescribe().fields.getMap();
for(string str: fList.keySet()){
fieldList.put(str, fList.get(str).getDescribe().getLabel());
}
string recordString;
for(string objmap : fieldList.keyset()){
recordString = key+','+objmap+','+fieldList.get(objmap)+'\n';
finalstr = finalstr +recordString;
}
finalstr = finalstr +recordString+'\n';
}else{
return;
}
}
}
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Report.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'email address'};
String subject ='object CSV';
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setPlainTextBody('body Text');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}