git_bsmd/ENI2/Excel/ExcelSimpleWriter.cs

60 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using bsmd.database;
namespace ENI2.Excel
{
static class ExcelSimpleWriter
{
public static void WriteMaerskList(string filename, List<MaerskData> data)
{
Application excelApp = new Application();
excelApp.DisplayAlerts = false;
excelApp.Visible = false;
Workbook wb = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = wb.Worksheets[1];
// ExcelWorkSheet.Cells[r, c] = "R" + r + "C" + c;
for (int i = 0; i < data.Count; i++)
{
MaerskData md = data[i];
ws.Cells[i + 1, 1] = md.ColA;
ws.Cells[i + 1, 2] = md.ColB;
ws.Cells[i + 1, 3] = md.ColC;
ws.Cells[i + 1, 4] = md.ColD;
ws.Cells[i + 1, 5] = md.ColE;
ws.Cells[i + 1, 6] = md.ColF;
ws.Cells[i + 1, 7] = md.ColG;
ws.Cells[i + 1, 8] = md.ColH;
ws.Cells[i + 1, 9] = md.ColI;
ws.Cells[i + 1, 10] = md.ColJ;
ws.Cells[i + 1, 11] = md.ColK;
ws.Cells[i + 1, 12] = md.ColL;
ws.Cells[i + 1, 13] = md.ColM ;
ws.Cells[i + 1, 14] = md.Remark;
}
wb.SaveAs(filename, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
wb.Saved = true;
wb.Close(0);
Marshal.ReleaseComObject(ws);
Marshal.ReleaseComObject(wb);
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
}
}
}