Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
4.3 kB
4
Indexable
Never
Using System.IO.File;
Using OfficeOpenXml;
Using OfficeOpenXml.ExcelPackage;
Using OfficeOpenXml.ExcelRange;

internal final class CeExportBonus
{
    public static void main(Args _args)
    {

        DialogField dialogBaseEnum;
        CeFiletypeEnum dialogValue;
        
        Dialog dialog=new Dialog("@MA:PickFileType");
        dialogBaseEnum = dialog.addField(extendedTypeStr(CeFiletypeEnum),"@MA:InputType");

        if(dialog.run() && dialog.closedOk())
        {
            dialogValue = dialogBaseEnum.value();
            if(dialogValue == FiletypeEnum::CSV)
            {
                CeExportBonus::ExportAsCsv();
            }
            if(dialogValue == FiletypeEnum::Excel)
            {
                CeExportBonus::ExportAsExcel();
            }
        }
    }

    public static void ExportAsExcel()
    {
        DocuFileSaveResult saveResult =
            DocuFileSave::promptForSaveLocation("@ApplicationPlatform:OfficeDefaultWorkbookFileName","@MA:ExcelFile", null, "@MA:ExportExcel");

        if (saveResult&& saveResult.parmAction() != DocuFileSaveAction::Cancel)
        {
            saveResult.parmOpenParameters('web=1');
            saveResult.parmOpenInNewWindow(false);

            CeBonustabell CeBonustabell;
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

            using(var package = new OfficeOpenXml.ExcelPackage(memoryStream))
            {

                var worksheets = package.get_Workbook().get_Worksheets();
                var worksheet = worksheets.Add("Sheet1");
                var cells = worksheet.get_Cells();
                var currentRow=1 ;

                var cell = cells.get_Item(currentRow,1);
                cell.set_Value("@MA:Id");
                cell = cells.get_Item(currentRow,2);
                cell.set_Value("@MA:Description");
                cell = cells.get_Item(currentRow,3);
                cell.set_Value("@MA:Bonus");
                cell = cells.get_Item(currentRow,4);
                cell.set_Value("@MA:Inactive");
                currentRow++;

                while select CeBonustabell
                {
                    cell= cells.get_Item(currentRow, 1);
                    cell.set_Value(CeBonustabell.Id);
                    cell= cells.get_Item(currentRow, 2);
                    cell.set_Value(CeBonustabell.Description);
                    cell= cells.get_Item(currentRow, 3);
                    cell.set_Value(CeBonustabell.BonusAmount);
                    cell= cells.get_Item(currentRow, 4);
                    cell.set_Value(CeBonustabell.Inactive);
                    currentRow++;
                }

                package.Save();
            }

            memoryStream.Seek(0,System.IO.SeekOrigin::Begin);
            DocuFileSave::processSaveResult(memoryStream,saveResult);

    
        }
    }

    public static void ExportAsCSV()
    {
        DocuFileSaveResult saveResult =
            DocuFileSave::promptForSaveLocation("@ApplicationPlatform:OfficeDefaultWorkbookFileName","@MA:CSV", null, "@MA:ExportCSV");

        if (saveResult&& saveResult.parmAction() != DocuFileSaveAction::Cancel)
        {
            saveResult.parmOpenParameters('web=1');
            saveResult.parmOpenInNewWindow(false);
            commaStreamIo iO = CommaStreamIo::constructForWrite();

            CeBonustabell BonusTabell;
            str fileName = "Export.csv";

            container header = ["@MA:Id", '@MA:Description', '@MA:Bonus', '@MA:Inactive'];
            iO.write(con2Str(header));
            header = conNull();

            while select Bonustabell
            {
                iO.write(strFmt("%1,%2,%3,%4", Bonustabell.Id,
                Bonustabell.Description,
                Bonustabell.BonusAmount,
                enum2Str(Bonustabell.Inactive)));
               
            }

            fileName = "Export.csv";
            System.IO.Stream stream = iO.getStream();
            stream.Position = 0;
            System.IO.StreamReader reader = new System.IO.StreamReader(stream);
            str csvFileContent = reader.ReadToEnd();
            File::SendStringAsFileToUser(csvFileContent,  filename);
        }
    }

}