|
 |
|
C# Windows Form การ Export Excel แต่พอเปิดไฟล์ดู HeadColumn ไม่มีแสดงให้ค่ะ |
|
 |
|
|
 |
 |
|
เป็นการนำข้อมูลที่โชว์หน้า Datagridview เซฟลงในไฟล์ Excel ค่ะ
อยากให้มี HeadColumn แสดงในไฟล์ Excel ด้วย ที่ทำได้ตอนนี้คือ มันโชว์แต่ข้อมูลอย่างเดียว แต่ไม่มีหัวข้อให้ค่ะ
รบกวนดู Code ให้ทีนะคะ
ต้องแก้ตรงไหน งงมาหลายวันมากเลยคะ T_T
Code (C#)
private void btExport_Click(object sender, EventArgs e)
{
string sql = @"SELECT id,Desc,SN,Type FROM tableAs";
SqlCommand cmd = new SqlCommand(sql, Conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "data");
DataTable dt = ds.Tables["data"];
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int r = 0;
int c = 0;
// DGV To EXCLE ได้ แต่ HeadColumn ยังไม่เซฟเข้ามาในไฟล์ด้วย
for (r = 0; r <= dataGridView1.RowCount - 1; r++)
{
for (c = 0; c <= dataGridView1.ColumnCount - 1; c++)
{
DataGridViewCell cell = dataGridView1[c, r];
xlWorkSheet.Cells[r + 1, c + 1] = cell.Value;
}
}
xlWorkBook.SaveAs(@"c:\details.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created");
}
ขอบคุณล่วงหน้ามากๆนะคะ
Tag : .NET, Excel (Excel.Application), Win (Windows App), LINQ, C#, VS 2010 (.NET 4.x)

|
|
 |
 |
 |
 |
Date :
2012-09-23 20:07:35 |
By :
porn_toon |
View :
1999 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณจะต้องสร้าง Column ที่เป็น Header ขึ้นมาเองครับ
|
 |
 |
 |
 |
Date :
2012-09-24 06:13:00 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|