简体   繁体   中英

How can I easily save dataGridView to .XLS or or .CSV without prompting user with saveFileDialog C# Winforms?

I am working on a project that will allow a user to view a MySQL database table in a C# Winform dataGridView.

I want the users to be able to save the dataGridView contents to c:\\temp\\export.xls without prompting the user with saveFileDialog (it is fine that it uses saveFileDialog, as long as it can bypass any user input and save as c:\\temp\\export.xls) with a button click. I need help modifying the second part to bypass user input.

The code I am using to save the data is in two parts (The code in the second part prompts the user for save location, but i want to omit that and have it automatic):

    private void ToCsV(DataGridView dGV, string filename)
    {
        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 - 1; 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(1254);
        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();
    } 

2nd part:

    private void button4_Click(object sender, EventArgs e)
    {

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel Documents (*.xls)|*.xls";
        sfd.FileName = "export.xls";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
          // ToCsV(dataGridView1, @"c:\temp\export.xls");   ???????
           ToCsV(dataGridView1, sfd.FileName); 
        }  

I want to modify the second part to save automatically @ c:\\temp\\export.xls. Hopefully i have explained my intentions well. I have looked into the NPOI library but not sure if this is what i need. It would be nice to do it in the background in case, for some reason, the user doesnt have excel installed.

Why not just do this, or if you want to run this in the back ground you can view here how to use the background worker class, http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

private void button4_Click(object sender, EventArgs e)
{
   //I would probabaly generate a time stamped Filename so not to over write existing files
   // You can use something like a background worker to run this in the back ground
   ToCsV(dataGridView1, @"c:\temp\export.xls");   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM