레트로코딩토끼토끼

D365_VisualStudio | Excel Export - ZIP File 본문

Dynamic365

D365_VisualStudio | Excel Export - ZIP File

쌉T타임 2026. 1. 31. 11:03
Excel Export 구조
   1) ExcelPackage (OfficeOpenXml)
   2) MemoryStream (System.IO)
   3) zip/Fild download
zipStream 내부
 ├─ file1.xlsx
 ├─ file2.xlsx
 ├─ file3.xlsx

 

  • SendFileToUser | 파일 1건 / 즉시 전송, 즉시 다운
  • Zip + Upload      | 여러 건 / 패키징 후 저장, 팝업

1. Declare namespace

: File/MemoryStream/Excel 생성 관련 기능 사용을 위한 네임스페이스 선언


using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
 

2. Encoding

: 한국어 깨짐 방지
System.Text.Encoding    encoding  = System.Text.Encoding::GetEncoding(949);
 

3. ZIP 파일 생성

System.IO.MemoryStream zipStream = new System.IO.MemoryStream();  //-> RAM
 

4. 압축 파일 다운로드

using(System.IO.Compression.ZipArchive zip
     = new System.IO.Compression.ZipArchive(zipStream, System.IO.Compression.ZipArchiveMode::Create, true))

 

5. 각 Excel File 생성

System.IO.MemoryStream fileStream;
 
1) Template File loadded
Template stream → copy → MemoryStream 생성
System.IO.Stream stream                         = documentManagement.fileToStream();
System.IO.MemoryStream templateCopy = new System.IO.MemoryStream();
stream.CopyTo(templateCopy);
templateCopy.Position = 0;

2) Create ExcelPackage
OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(templateCopy);
ExcelWorksheets  excelWorksheets   = package.get_Workbook().get_Worksheets();
ExcelWorksheet   sheet                      = excelWorksheets.get_Item(1);  // 기존 템플릿 시트에 덮어쓰기
ExcelRange          cells                       = sheet.Cells;
 
3) Input value of Cell
...
cells.get_Item(4, 10).set_Value();
...
 
4) Save in MemoryStream
System.IO.MemoryStream resultStream = new System.IO.MemoryStream();
package.SaveAs(resultStream);
resultStream.Position = 0;


5. Add to zip file
ZipArchive + Entry + Upload (패키징>업로드>링크생성)

// ZIP File Entry 생성(파일명만 생성)
System.IO.Compression.ZipArchiveEntry entry = zip.CreateEntry(fileName);

 
 //-> 엑셀 MemoryStream을 ZIP File로 복사
using( System.IO.Stream  entryStream = entry.Open())
{
    fileStream.Seek(0, System.IO.SeekOrigin::Begin)//-> Excel 파일 처음부터 다시 읽기, 다운 시 필수
    fileStream.CopyTo(entryStream);
}
fileStream.Close();

 

6. ZIP File load

zipStream.Seek(0, System.IO.SeekOrigin::Begin);  //-> ZIP 파일 처음부터 다시 읽기(업로드 파일 크기 0 방지), 다운 시 필수
FileUploadTemporaryStorageStrategy uploadStrategy = new FileUploadTemporaryStorageStrategy();
FileUploadTemporaryStorageResult    uploadResult    = uploadStrategy.uploadFile(zipStream,
                                                                                          strFmt( todayStr_ver2 + '.zip'),
                                                                                          'application/zip',
                                                                                          '.zip');  //->ZIP File을 D365 임시 저장소에 업로드

if (uploadResult && uploadResult.getUploadStatus())
{
    // 팝업 허용 필수
    new Browser().navigate(uploadResult.getDownloadUrl());
    info('File download complate.');
} //-> User 다운로드 링크 제공

 
 

Result