Exporting data to Excel through X++ code
Sometimes we need to
export data from Microsoft Dynamics 365 to Excel using x++ code.
Through this post can
see how to export data from form to excel
class SMJ_ExportData
{
public static void main(Args args)
{
PurchParmLine
PurchParmLine,PurchParmLineSelect;
PurchParmLine = args.record();
DocuFileSaveResult saveResult = DocuFileSave::promptForSaveLocation("@ApplicationPlatform:Testingexcelexport", "xlsx", null, "Testing excel export");
if (saveResult && saveResult.parmAction() != DocuFileSaveAction::Cancel)
{
saveResult.parmOpenParameters('web=1');
saveResult.parmOpenInNewWindow(false);
System.IO.Stream workbookStream = new System.IO.MemoryStream();
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
using (var package = new OfficeOpenXml.ExcelPackage(memoryStream))
{
var currentRow=1;
var worksheets = package.get_Workbook().get_Worksheets();
var worksheet = worksheets.Add("First
sheet");
var cells = worksheet.get_Cells();
var cell = cells.get_Item(currentRow,1);
System.String value="SNo.";
cell.set_Value(value);
cell=null;
value="Purchae order";
cell=cells.get_Item(currentRow,2);
cell.set_Value(value);
cell=null;
value="Line number";
cell=cells.get_Item(currentRow,3);
cell.set_Value(value);
cell=null;
value="Item number";
cell=cells.get_Item(currentRow,4);
cell.set_Value(value);
cell=null;
value="Procurement category";
cell=cells.get_Item(currentRow,5);
cell.set_Value(value);
cell=null;
value="Text";
cell=cells.get_Item(currentRow,6);
cell.set_Value(value);
cell=null;
value="Site";
cell=cells.get_Item(currentRow,7);
cell.set_Value(value);
cell=null;
value="Warehouse";
cell=cells.get_Item(currentRow,8);
cell.set_Value(value);
cell=null;
value="CW update";
cell=cells.get_Item(currentRow,9);
cell.set_Value(value);
cell=null;
value="Quantity";
cell=cells.get_Item(currentRow,10);
cell.set_Value(value);
cell=null;
value="Unit price";
cell=cells.get_Item(currentRow,11);
cell.set_Value(value);
cell=null;
value="Vendor batch date";
cell=cells.get_Item(currentRow,12);
cell.set_Value(value);
cell=null;
value="Line net amount";
cell=cells.get_Item(currentRow,13);
cell.set_Value(value);
cell=null;
value="Vendor expiry date";
cell=cells.get_Item(currentRow,14);
cell.set_Value(value);
cell=null;
value="Quality order status";
cell=cells.get_Item(currentRow,15);
cell.set_Value(value);
while select PurchParmLineSelect
where PurchParmLineSelect.OrigPurchId == PurchParmLine.OrigPurchId
&& PurchParmLine.SMJ_SNo != ""
where PurchParmLineSelect.OrigPurchId == PurchParmLine.OrigPurchId
&& PurchParmLine.SMJ_SNo != ""
{
currentRow ++;
cell=null;
cell=cells.get_Item(currentRow,1);
cell.set_Value(PurchParmLineSelect.SMJ_SNo);
cell=null;
cell=cells.get_Item(currentRow,2);
cell.set_Value(PurchParmLineSelect.OrigPurchId);
cell=null;
cell=cells.get_Item(currentRow,3);
cell.set_Value(PurchParmLineSelect.PurchaseLineLineNumber);
cell=null;
cell=cells.get_Item(currentRow,4);
cell.set_Value(PurchParmLineSelect.ItemId);
cell=null;
cell=cells.get_Item(currentRow,5);
cell.set_Value(PurchParmLineSelect.ProcurementCategory);
cell=null;
cell=cells.get_Item(currentRow,6);
cell.set_Value(PurchParmLineSelect.name());
cell=null;
cell=cells.get_Item(currentRow,7);
cell.set_Value(InventDim::find(PurchParmLineSelect.InventDimId).InventSiteId);
cell=null;
cell=cells.get_Item(currentRow,8);
cell.set_Value(InventDim::find(PurchParmLineSelect.InventDimId).InventLocationId);
cell=null;
cell=cells.get_Item(currentRow,9);
cell.set_Value(PurchParmLineSelect.PdsCWReceiveNow);
cell=null;
cell=cells.get_Item(currentRow,10);
cell.set_Value(PurchParmLineSelect.ReceiveNow);
cell=null;
cell=cells.get_Item(currentRow,11);
cell.set_Value(PurchParmLineSelect.PurchPrice);
cell=null;
cell=cells.get_Item(currentRow,12);
cell.set_Value(PurchParmLineSelect.PdsVendBatchDate);
cell=null;
cell=cells.get_Item(currentRow,13);
cell.set_Value(PurchParmLineSelect.LineAmount);
cell=null;
cell=cells.get_Item(currentRow,14);
cell.set_Value(PurchParmLineSelect.PdsVendExpiryDate);
cell=null;
cell=cells.get_Item(currentRow,15);
cell.set_Value(PurchParmLineSelect.qualityOrderStatusDisplay());
cell=null;
}
package.Save();
}
memoryStream.Seek(0, System.IO.SeekOrigin::Begin);
DocuFileSave::processSaveResult(memoryStream, saveResult);
}
}
}
I tried running this code unfortunately, nothing happened when I clicked the download button. I am new to dynamics 365 x++ .
ReplyDelete