How to Export Simple Product CSV from Magento 2 Programmatically

How to Export Simple Product CSV Programmatically using custom Scripts.
Create one custom.php File into the root folder.
and put below code into a file and run into the browser.
You can find this CSV inside the root directory
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('memory_limit', '5G'); error_reporting(E_ALL); use Magento\Framework\App\Bootstrap; require realpath(__DIR__) . '/../app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap--->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('admin'); $registry = $objectManager->get('Magento\Framework\Registry'); $registry->register('isSecureArea', true); //Store id of exported products, This is useful when we have multiple stores. $store_id = 4; ("Your Store ID") $fp = fopen("export-simple.csv","w+"); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection'); /** Apply filters here */ $collection = $productCollection->addAttributeToSelect('*')->addFieldTofilter('type_id','simple') ->addFieldToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)->load(); foreach ($collection as $product) { $data = array(); //$data[] = $product->getTypeId(); $data[] = $product->getName(); $data[] = $product->getSku(); fputcsv($fp, $data);
if ($product->getSize() > 0) {
$data = array();
foreach ($products as $product) {
//print_r($product->getData()); ("Here You get all Data from collection")
$data[] = $child->getName();
$data[] = $child->getSku();
fputcsv($fp, $data);
}
}
} fclose($fp);