|
|
|
ขอสอบถามเกี่ยวกับการ Export หลาย gridviews เป็น Excel เป็นหลายๆ Worksheet ครับ |
|
|
|
|
|
|
|
คือตอนนี้ผมเอาโค้สมาจากเว็ปนี้
http://stackoverflow.com/questions/30359299/export-multiple-gridviews-to-multiple-excel-tabs-sheets
ผมอยาก export ให้เป็นแบบ 1 ไฟล์มีหลาย worksheet ตอนนี้ผมลองปรับโค้สดูได้ตามข้างล่างแต่พอตอน Export
ออกมามันออกมาเเค่อันเดียวอะครับ T_T ขอบพระคุณล่วงหน้าครับ
Code (C#)
SqlDataAdapter da3 = new SqlDataAdapter();
SqlCommand cmd3 = new SqlCommand();
cmd3.CommandText = "SELECT a.Date_Time,a.Value_Per_15min AS WL FROM TStatus a" + " "
+ "WHERE a.TagID in ('03020') AND a.Date_Time >= @p_StartDate AND a.Date_Time < @p_EndDate";
cmd3.Parameters.Add("@p_StartDate", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;
cmd3.Parameters.Add("@p_EndDate", SqlDbType.DateTime).Value = dateTimePicker2.Value.Date.AddDays(1);
cmd3.Connection = conn;
da3.SelectCommand = cmd3;
da3.Fill(ds3, "TSEACH3");
dv3 = new DataView(ds3.Tables["TSEACH3"]);
dataGridView1.DataSource = ds3.Tables["TSEACH3"];
SqlDataAdapter da7 = new SqlDataAdapter();
SqlCommand cmd7 = new SqlCommand();
cmd7.CommandText = "SELECT a.Date_Time,a.Value_Per_15min AS WL FROM TStatus a" + " "
+ "WHERE a.TagID in ('07020') AND a.Date_Time >= @p_StartDate AND a.Date_Time < @p_EndDate";
cmd7.Parameters.Add("@p_StartDate", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;
cmd7.Parameters.Add("@p_EndDate", SqlDbType.DateTime).Value = dateTimePicker2.Value.Date.AddDays(1);
cmd7.Connection = conn;
da7.SelectCommand = cmd7;
da7.Fill(ds7, "TSEACH7");
dv7 = new DataView(ds7.Tables["TSEACH7"]);
dataGridView2.DataSource = ds7.Tables["TSEACH7"];
Export();
}
}
}
private void Export()
{
Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
try
{
// worksheet.Name = "ExportedFromDatGrid";
DataGridView[] gvExcel = new DataGridView[] { dataGridView1, dataGridView2 };
string[] name = new string[] { "Name1", "Name2"};
int cellRowIndex = 1;
int cellColumnIndex = 1;
for (int ii = 0; ii < gvExcel.Length; ii++)
{
worksheet = workbook.ActiveSheet;
worksheet.Name = name[ii].ToString();
//Loop through each row and read value from each column.
for (int i = 0; i < gvExcel[ii].Rows.Count - 1; i++)
{
for (int j = 0; j < gvExcel[ii].Columns.Count; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
if (cellRowIndex == 1)
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = gvExcel[ii].Columns[j].HeaderText;
}
else
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = gvExcel[ii].Rows[i].Cells[j].Value.ToString();
}
cellColumnIndex++;
}
cellColumnIndex = 1;
cellRowIndex++;
}
}
//Getting the location and file name of the excel to save from user.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.FileName = comboBox1.SelectedItem.ToString() + " วันที่ " + dateTimePicker1.Value.ToString("dd-MMM-yyyy") + " ถึง " + dateTimePicker2.Value.ToString("dd-MMM-yyyy");
saveDialog.Filter = "Excel Workbooks (*.xlsx)|*.xlsx|Excel 97-2003 Workbooks (*.xls)|*.xls|CSV (Comma delimited)(*.csv)|.csv|All files (*.*)|*.*";
saveDialog.FilterIndex = 1;
if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show("Export Successful");
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit();
workbook = null;
excel = null;
}
}
Tag : C#
|
|
|
|
|
|
Date :
2016-10-26 13:55:15 |
By :
tumy9468 |
View :
1057 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|