Saving data for excel sheet in winform vb.net application

A year ago, I saw a beautiful simple code that gets a data table and saves it in an excel file.

The trick was using a web library (something with http), and I'm pretty sure it is a stream.

I find a lot of code with the answer, but I can't get it to work in a win-form environment. There is also a cell by cell code - uninteresting - slower.

I want to insert it as a range or something close.

thanks

+3
source share
5 answers

I believe this is the code you are looking for:

DataTable in Excel

He is using HtmlTextWriter.

+2
source

, ​​.

, , CSV Excel.

0

, , , . / Excel.

Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
    If dgvDisplaySet.GetClipboardContent Is Nothing Then
        MsgBox("Nothing selected to copy to clipboard.")
    Else
        Clipboard.SetDataObject(dgvDisplaySet.GetClipboardContent)
    End If
End Sub
0

Jay

, :

, ;)

private void cmdSaveToExcel_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = saveFileDialog1.FileName;               
            }

            // create the DataGrid and perform the databinding
            System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;


            if (connDBs != null && rtxtCode.Text != "")
            {
                DataTable dt;
                dt = connDBs.userQuery(rtxtCode.Text); // getting a table with one column of the databases names
                //grdData.DataSource = dt;
                grid.DataSource = dt;
                // grid.DataMember = data.Stats.TableName;

                grid.DataBind();

                // render the DataGrid control to a file
                using (StreamWriter sw = new StreamWriter(txtPath.Text))
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        grid.RenderControl(hw);
                    }
                }
                MessageBox.Show("The excel file was created successfully");
            }
            else
            {
                MessageBox.Show("Missing connection or query");
            }
        }
0

datatable ADO, RoundFromRecordset Range. . http://www.codeproject.com/KB/database/DataTableToRecordset.aspx

0

Source: https://habr.com/ru/post/1715631/


All Articles