|
 |
|
C# DataGridView - Export to Excel อยากทราบวิธี SUM ข้อมูลที่ด้านล่างสุดของตารางครับ |
|
 |
|
|
 |
 |
|
ต้องดูว่า ดึงไปเป็น Excel ด้วยเครื่องมืออะไรครับ
แล้วก็ไปเพิ่มสูตรที่แถวล่างสุดเอา
คิดว่าน่าจะประมาณนี้น่ะครับ
เพราะผมก็ไม่เคยทำ 55555
|
 |
 |
 |
 |
Date :
2013-09-13 11:40:06 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผม Export แบบนี้ครับ
Code (C#)
private void Excel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (dataGridView4.DataSource == null)
{
MessageBox.Show("ไม่มีข้อมูลที่จะแสดง");
return;
}
Excel.Application excel;
Excel.Workbook wbook;
Excel.Worksheet wsheet;
excel = new Excel.Application();
wbook = excel.Workbooks.Add();
wsheet = wbook.ActiveSheet;
excel.Visible = true;
string title = "ประวัติการขายสินค้าของ " + txthname.Text + " " + txtfname.Text + " " + txtlname.Text;
wsheet.Range["A1", "H1"].Merge();
wsheet.Range["A1", "H1"].Value = title;
wsheet.Range["A1", "H1"].Font.Bold = 1;
wsheet.Range["A3", "H3"].Font.Bold = 1;
wsheet.Range["A1", "H1"].HorizontalAlignment = -4108;
wsheet.Range["A3","A9999"].HorizontalAlignment = -4108;
wsheet.Range["B3","B9999"].HorizontalAlignment = -4108;
wsheet.Range["C3"].HorizontalAlignment = -4108;
wsheet.Range["D3"].HorizontalAlignment = -4108;
wsheet.Range["G3"].HorizontalAlignment = -4108;
wsheet.Range["E3"].HorizontalAlignment = -4108;
wsheet.Range["D3", "D9999"].HorizontalAlignment = -4108;
wsheet.Range["F3", "F9999"].HorizontalAlignment = -4108;
wsheet.Range["H3", "H9999"].HorizontalAlignment = -4108;
wsheet.Range["B3", "B9999"].ColumnWidth = 10;
wsheet.Range["C3", "C9999"].ColumnWidth = 13;
wsheet.Range["D3", "D9999"].ColumnWidth = 15;
wsheet.Range["E3", "E9999"].ColumnWidth = 14;
wsheet.Range["F3", "F9999"].ColumnWidth = 18;
wsheet.Range["G3", "G9999"].ColumnWidth = 15;
wsheet.Range["H3", "H9999"].ColumnWidth = 18;
wsheet.Range["C3", "C9999"].NumberFormat="#,##0.00";
wsheet.Range["E3", "E9999"].NumberFormat = "#,##0.00";
wsheet.Range["D3", "D9999"].NumberFormat = "#,##0.00";
wsheet.Range["G3", "G9999"].NumberFormat = "#,##0.00";
for (int i = 0; i < this.dataGridView4.Columns.Count; i++)
{
wsheet.Cells[3, i + 1] = this.dataGridView4.Columns[i].HeaderText;
}
for (int i = 0; i < this.dataGridView4.Rows.Count; i++)
{
DataGridViewRow row = this.dataGridView4.Rows[i];
for (int j = 0; j < row.Cells.Count; j++)
{
DataGridViewCell cell = row.Cells[j];
try
{
wsheet.Cells[i + 4, j + 1] = (cell.Value == null) ? "" : cell.Value.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
แต่ผมไม่เข้าใจว่าจะทำให้ SUM ลง Cells ล่างสุดยังไงครับ
|
 |
 |
 |
 |
Date :
2013-09-13 12:23:15 |
By :
jjj |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง add formula เข้าไปดูครับ
http://stackoverflow.com/questions/6427559/writing-excel-formula-using-vb-net
http://social.msdn.microsoft.com/Forums/office/en-US/86c19a53-e506-44f0-a7e9-45945a38654a/vbnet-excel-interop-autosum-via-automation
|
 |
 |
 |
 |
Date :
2013-09-13 12:43:58 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็น่าจะเป็น
Code (C#)
wsheet.Range["C" +(string)(3+this.dataGridView4.Rows.Count+1)].Formula = "Sum(C3:C" + (string)(3+this.dataGridView4.Rows.Count) + ")"
ลองดูน่ะครับ ผมมั่วเอา
|
ประวัติการแก้ไข 2013-09-13 15:00:06
 |
 |
 |
 |
Date :
2013-09-13 14:59:15 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|