I
I
Ivan2016-04-01 12:37:18
SQL
Ivan, 2016-04-01 12:37:18

How to export data from ms sql to excel?

Good afternoon.
There is ms sql server 2012 and there is excel 2007, you need to export data to excel. the standard data import import tool does not allow you to select excel in the second step. databases only.
I found a video on msdn how to do it through visual studio. but there it is old and now there is only 2012. everything went through the steps, plugging in at the last moment. names of columns in excel registers and further does not want.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2016-04-01
@fox_12

Have you tried saving to csv?
stackoverflow.com/questions/14212641/export-table-...

D
DartAlex, 2016-04-01
@DartAlex

If there is MS SQL Server Management Studio, then we make a request, Ctrl + A, Ctrl + C, Ctrl + V in Excel.
If through Visual Studio, we load the data into a DataTable or DataGridView and...

using ClosedXML.Excel;
...
private void export_excel()
{
    string FileName;

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "xls files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
    saveFileDialog1.Title = "Экспорт в Excel";
    saveFileDialog1.FileName = this.Text + " (" + DateTime.Now.ToString("yyyy-MM-dd") + ")";

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
         FileName = saveFileDialog1.FileName;
         var workbook = new XLWorkbook();
         var worksheet = workbook.Worksheets.Add(this.Text);
         for (int i = 0; i < DG.Columns.Count; i++)
         {
            worksheet.Cell(1, i + 1).Value = DG.Columns[i].Name;
         }

         for (int i = 0; i < DG.Rows.Count; i++)
         {
              for (int j = 0; j < DG.Columns.Count; j++)
              {
                  worksheet.Cell(i + 2, j + 1).Value = DG.Rows[i].Cells[j].Value.ToString();
              }
          }
          worksheet.Columns().AdjustToContents();
          workbook.SaveAs(FileName);
          MessageBox.Show("Экспорт завершён");
      }
}

It is necessary to add ClosedXML.dll, DocumentFormatOpenXml.dll to the project

A
Aleksey Ratnikov, 2016-04-01
@mahoho

I don't know how you look at the destination of the upload, there is Excel in the list (SSMS 2012, SQL Server 2012):
845497cac58f4312b7be95db36c41afc.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question