|
|
|
อยากสอบถามการใช้งาน Linq ผม select data ออกมาแล้วพอเอาลงในไฟล์ excel มันติดคอลั่มอื่นออกมาด้วย |
|
|
|
|
|
|
|
อยากสอบถามการใช้งาน Linq ผม select data ออกมาแล้วพอเอาลงในไฟล์ excel มันติดคอลั่มอื่นออกมาด้วย จะต้องทำยังไงหรอครับไม่ให้ คอลั่มที่ไม่ต้องการออกมาแสดงผล
Code (C#)
public void exportExcel()
{
try
{
List<MONote> listValue = new List<MONote>();
listValue = Session["listMoNote"] as List<MONote>;
listValue = listValue.Select(x => new MONote
{
MONo = x.MONo,
CostCenterDesc = x.CostCenterDesc,
CostCenterCode = x.CostCenterCode,
MODate = x.MODate,
MaterialCode = x.MaterialCode,
MaterialName = x.MaterialName,
WeightUnit = x.WeightUnit,
QtyBUn = x.QtyBUn,
QtyKG = x.QtyKG,
QtyCar = x.QtyCar,
OperatingHr = x.OperatingHr,
ManPower = x.ManPower,
ManHr = x.ManHr
}).ToList();
ExcelPackage excel = new ExcelPackage();
var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
workSheet.Cells[1, 1].LoadFromCollection(listValue, true);
using (var memoryStream = new MemoryStream())
{
//workSheet.Cells["A1"].Value = "MO / Mat Code";
////workSheet.Cells["A2"].Value = "MONo";
//workSheet.Cells["B1"].Value = "Cost Center Description";
//workSheet.Cells["C1"].Value = "Cost Center";
//workSheet.Cells["D1"].Value = "Posting Date";
////workSheet.Cells["A5"].Value = "Document Header Text";
//workSheet.Cells["E1"].Value = "Material";
//workSheet.Cells["F1"].Value = "Material Description";
//workSheet.Cells["G1"].Value = "Base Unit of Measure";
//workSheet.Cells["H1"].Value = "Sum of Quantity";
//workSheet.Cells["I1"].Value = "QTY(KG)";
//workSheet.Cells["J1"].Value = "QTY(Car)";
//workSheet.Cells["K1"].Value = "Operating Hour (Minute)";
//workSheet.Cells["L1"].Value = "Man Power";
//workSheet.Cells["M1"].Value = "Man Hour (Minute)";
workSheet.Column(4).Style.Numberformat.Format = "dd/MM/yyyy";
workSheet.Cells["A:AZ"].AutoFitColumns();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=testExport.xlsx");
excel.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{
throw ex;
}
}
Tag : .NET, Excel (Excel.Application), LINQ, MVC, C#, Windows
|
|
|
|
|
|
Date :
2021-11-18 13:42:29 |
By :
snowman0020 |
View :
527 |
Reply :
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|