 |
|
อยากข้อมูลที่เจนได้ในexcel มันตรงกับ SQL ครับ
ข้อมูลใน SQL

ข้อมูลที่เจนไฟล์ excel

โค้ดที่ใช้ในการเจน
Code (C#)
private void button5_Click(object sender, EventArgs e)//
{
try
{
Connection.connnectsql2();
SqlConnection constring = new SqlConnection(Connection.strcon2);
constring.Open();
constring.Close();
DateTime dtt = DateTime.Now;;
DataTable dt = Connection.executsql2("SELECT * FROM A_file");
dataGridView1.DataSource = dt;
GenExcel();
}
catch (Exception ex)
{
AutoClosingMessageBox.Show(ex.Message, "Error", 2000);
}
Code (C#)
public void GenExcel() {
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (*.xls)|*.xls";
sfd.FileName = "export.xls";
//DateTime.Now.Ticks.ToString();
//string strdtrp = "";
//strdtrp = dtrp.ToString("ddMM");
ToCsV(dataGridView1, @"D:\RP601" + DateTime.Now.ToString("ddMM") + ".xls");
AutoClosingMessageBox.Show("Send to Excel", "Success", 2000);
dataGridView1.Columns.Clear();
dataGridView1.Refresh();
}
Code (C#)
private void ToCsV(DataGridView dGV, string filename)//ฟังชั่นของ excel
{
string stOutput = "";
// Export titles:
string sHeaders = "";
for (int j = 0; j < dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
// Export data.
for (int i = 0; i < dGV.RowCount; i++)
{
string stLine = "";
for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
Encoding utf16 = Encoding.GetEncoding(874);
byte[] output = utf16.GetBytes(stOutput);
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
Tag : .NET, Win (Windows App), C#
|
|
 |
 |
 |
 |
Date :
2018-01-11 12:40:38 |
By :
darkgolfman0 |
View :
2952 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |