|
|
|
ขอสอบถามเรื่อง C# ในการใช้ EPPlus เพื่อป้อนค่าใน excel ครับ |
|
|
|
|
|
|
|
จากโค้ด
Code (C#)
private void button4_Click(object sender, EventArgs e)
{
string _FileBlnk = fileFH_802;
string _FileRun = Path.Combine(Application.StartupPath, $"files\\FH_802_{comboBox1.Text}_{dateTimePicker1.Value.ToString("yyyyMMdd hhmmssffff")}.xlsx");
System.IO.File.Copy(_FileBlnk, _FileRun);
System.Threading.Thread.Sleep(500);
OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(new System.IO.FileInfo(_FileRun));
OfficeOpenXml.ExcelWorksheet sheet = package.Workbook.Worksheets[1];
sheet.Cells["E10"].Value = dateTimePicker1.Value.ToString("dd/MM/yyyy");
sheet.Cells["D11"].Value = comboBox1.Text;
sheet = package.Workbook.Worksheets[3];
for (int i = 1; i <= 20; i++)
{
sheet.Cells["D" + i + 2].Value = dataGridView1[i+1,0].Value;
sheet.Cells["E" + i + 2].Value = dataGridView1[i + 1, 1].Value;
sheet.Cells["F" + i + 2].Value = dataGridView1[i + 1, 2].Value;
sheet.Cells["G" + i + 2].Value = dataGridView1[i + 1, 3].Value;
}
sheet = package.Workbook.Worksheets[2];
sheet.Cells["C83"].Value = dataGridView2[3, 0].Value;
sheet.Cells["D83"].Value = dataGridView2[4, 0].Value;
sheet.Cells["E83"].Value = dataGridView2[5, 0].Value;
sheet.Cells["F83"].Value = dataGridView2[6, 0].Value;
sheet.Cells["G83"].Value = dataGridView2[7, 0].Value;
sheet.Cells["C84"].Value = dataGridView2[8, 0].Value;
sheet.Cells["D84"].Value = dataGridView2[9, 0].Value;
sheet.Cells["E84"].Value = dataGridView2[10, 0].Value;
sheet.Cells["F84"].Value = dataGridView2[11, 0].Value;
sheet.Cells["G84"].Value = dataGridView2[12, 0].Value;
sheet.Cells["C85"].Value = dataGridView2[3, 1].Value;
sheet.Cells["D85"].Value = dataGridView2[4, 1].Value;
sheet.Cells["E85"].Value = dataGridView2[5, 1].Value;
sheet.Cells["F85"].Value = dataGridView2[6, 1].Value;
sheet.Cells["G85"].Value = dataGridView2[7, 1].Value;
sheet.Cells["C86"].Value = dataGridView2[8, 1].Value;
sheet.Cells["D86"].Value = dataGridView2[9, 1].Value;
sheet.Cells["E86"].Value = dataGridView2[10, 1].Value;
sheet.Cells["F86"].Value = dataGridView2[11, 1].Value;
sheet.Cells["G86"].Value = dataGridView2[12, 1].Value;
sheet.Cells["C87"].Value = dataGridView2[3, 2].Value;
sheet.Cells["D87"].Value = dataGridView2[4, 2].Value;
sheet.Cells["E87"].Value = dataGridView2[5, 2].Value;
sheet.Cells["F87"].Value = dataGridView2[6, 2].Value;
sheet.Cells["G87"].Value = dataGridView2[7, 2].Value;
sheet.Cells["C88"].Value = dataGridView2[8, 2].Value;
sheet.Cells["D88"].Value = dataGridView2[9, 2].Value;
sheet.Cells["E88"].Value = dataGridView2[10, 2].Value;
sheet.Cells["F88"].Value = dataGridView2[11, 2].Value;
sheet.Cells["G88"].Value = dataGridView2[12, 2].Value;
sheet.Cells["C121"].Value = dataGridView3[1, 0].Value;
sheet.Cells["C122"].Value = dataGridView3[2, 0].Value;
sheet.Cells["C123"].Value = dataGridView3[3, 0].Value;
sheet.Cells["C124"].Value = dataGridView3[4, 0].Value;
sheet.Cells["C125"].Value = dataGridView3[1, 1].Value;
sheet.Cells["C126"].Value = dataGridView3[2, 1].Value;
sheet.Cells["C127"].Value = dataGridView3[3, 1].Value;
sheet.Cells["C128"].Value = dataGridView3[4, 1].Value;
sheet.Cells["C129"].Value = dataGridView3[1, 2].Value;
sheet.Cells["C130"].Value = dataGridView3[2, 2].Value;
sheet.Cells["C131"].Value = dataGridView3[3, 2].Value;
sheet.Cells["C132"].Value = dataGridView3[4, 2].Value;
sheet.Cells["C133"].Value = dataGridView3[1, 3].Value;
sheet.Cells["C134"].Value = dataGridView3[2, 3].Value;
sheet.Cells["C135"].Value = dataGridView3[3, 3].Value;
sheet.Cells["C136"].Value = dataGridView3[4, 3].Value;
sheet.Cells["C137"].Value = dataGridView3[1, 4].Value;
sheet.Cells["C138"].Value = dataGridView3[2, 4].Value;
sheet.Cells["C139"].Value = dataGridView3[3, 4].Value;
sheet.Cells["C140"].Value = dataGridView3[4, 4].Value;
sheet.Cells["E159"].Value = dataGridView2[2, 0].Value;
sheet.Cells["E169"].Value = dataGridView2[2, 1].Value;
sheet.Cells["E179"].Value = dataGridView2[2, 2].Value;
package.Save();
System.Threading.Thread.Sleep(500);
System.Diagnostics.Process.Start(_FileRun);
System.Threading.Thread.Sleep(100);
}
ป้อนได้ค่า แต่ค่าที่ป้อนเข้าไป กลับไม่ทำให้สูตรคำนวณใน excel ทำงาน ครับ
ต้องมาปรับค่าใน excel ถึงจะคำนวณให้ ไม่ทราบว่า ต้องแก้โค้ดอย่างไร ครับ
Tag : .NET, Win (Windows App), C#
|
|
|
|
|
|
Date :
2024-01-10 22:55:30 |
By :
lamaka.tor |
View :
250 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขออนุญาต ปิดกระทู้ ครับ
ใช้ double.Parse เช่น
sheet.Cells["C83"].Value = double.Parse( dataGridView2[3, 0].Value.ToString());
ก็ใช้ได้เลย ครับ
|
|
|
|
|
Date :
2024-01-10 22:59:10 |
By :
lamaka.tor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|