Vb.net DataGrid导出到excel

Vb.net DataGrid导出到excel,vb.net,datagrid,.net-1.1,Vb.net,Datagrid,.net 1.1,我有一个DataGrid控件,它用dataset填充 我没有显示DataGrid控件中数据集的所有字段 我想从我的DataGrid创建一个excel文件 如何获得解决方案 (windows窗体,VBNET1.1)可能在这项主要工作中 String path = @"D:\users\...."; //your path String connStr = "Provider=//your provider;Data Source=" + path + ";Extended Prop

我有一个
DataGrid控件
,它用
datase
t填充

我没有显示DataGrid控件中数据集的所有字段

我想从我的
DataGrid
创建一个
excel文件

如何获得解决方案


(windows窗体,
VBNET1.1

可能在这项主要工作中

  String path = @"D:\users\....";
    //your path


String connStr = "Provider=//your provider;Data Source=" + path + ";Extended Properties=Excel 12.0;";

            //The connection to that file

            OleDbConnection conn = new OleDbConnection(connStr);

            //The query

            string strSQL = "SELECT * FROM [?]";

            //The command 

            OleDbCommand cmd = new OleDbCommand(/*The query*/strSQL, /*The connection*/conn);

            DataTable dT = new DataTable();

            conn.Open();

            try

            {

                OleDbDataReader dR = cmd.ExecuteReader();

                dT.Load(dR);

                bS.DataSource = dT;

                dGV.DataSource = bS;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                conn.Close();

            }

也许在这个原则的工作

  String path = @"D:\users\....";
    //your path


String connStr = "Provider=//your provider;Data Source=" + path + ";Extended Properties=Excel 12.0;";

            //The connection to that file

            OleDbConnection conn = new OleDbConnection(connStr);

            //The query

            string strSQL = "SELECT * FROM [?]";

            //The command 

            OleDbCommand cmd = new OleDbCommand(/*The query*/strSQL, /*The connection*/conn);

            DataTable dT = new DataTable();

            conn.Open();

            try

            {

                OleDbDataReader dR = cmd.ExecuteReader();

                dT.Load(dR);

                bS.DataSource = dT;

                dGV.DataSource = bS;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                conn.Close();

            }
试试这个

试试这个

Imports Excel = Microsoft.Office.Interop.Excel

Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

 wBook = excel.Workbooks.Add(System.Reflection.Missing.Value)
wSheet = wBook.Sheets("sheet1")

  With wBook
.Sheets("Sheet1").Select()
.Sheets(1).Name = "NameYourSheet"
End With


For i = 0 To DataGrid1.RowCount - 1
For j = 0 To DataGrid1.ColumnCount - 1
wSheet.Cells(i + 1, j + 1).value = DataGrid1.Rows(i).Cells(j).Value.tosring
Next j
Next i

wSheet.Columns.AutoFit()
试试这个

试试这个

Imports Excel = Microsoft.Office.Interop.Excel

Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

 wBook = excel.Workbooks.Add(System.Reflection.Missing.Value)
wSheet = wBook.Sheets("sheet1")

  With wBook
.Sheets("Sheet1").Select()
.Sheets(1).Name = "NameYourSheet"
End With


For i = 0 To DataGrid1.RowCount - 1
For j = 0 To DataGrid1.ColumnCount - 1
wSheet.Cells(i + 1, j + 1).value = DataGrid1.Rows(i).Cells(j).Value.tosring
Next j
Next i

wSheet.Columns.AutoFit()
试一试

试一试

试试这个:

Sub create_excel(sender As Object, e As EventArgs)
    Dim strFileName As string 
    Dim tw As New StringWriter() 
    Dim hw As New HtmlTextWriter(tw)    

    strFileName = "some_excel_from_datagrid.xls"
    Response.ContentType = "application/vnd.msexcel"
    Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName)
    Response.Charset = "UTF-8"
    Response.ContentEncoding = Encoding.Default

    DataGridID.RenderControl(hw)

    Response.Write(tw.ToString())
    Response.End() 
End Sub
试试这个:

Sub create_excel(sender As Object, e As EventArgs)
    Dim strFileName As string 
    Dim tw As New StringWriter() 
    Dim hw As New HtmlTextWriter(tw)    

    strFileName = "some_excel_from_datagrid.xls"
    Response.ContentType = "application/vnd.msexcel"
    Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName)
    Response.Charset = "UTF-8"
    Response.ContentEncoding = Encoding.Default

    DataGridID.RenderControl(hw)

    Response.Write(tw.ToString())
    Response.End() 
End Sub

这是什么??它不是一种导出方法:(简易方法:不使用catch和try。这是什么?)它不是一种导出方法:(简易方法:不使用catch和try。是否要从datagrid导出excel?是否要从datagrid导出excel?如果没有,请先检查文件是否已创建,然后使用“if System.IO.Directory.Exists(tempPath)”创建它=False然后是System.IO.Directory.CreateDirectory(tempPath),如果System.IO.File.Exists(strFileName)=False然后是System.IO.File.Create(strFileName)如果End如果End如果`where strFileName是文件名,tempPath只是excel文件所在的文件夹,则如何在不使用Microsoft.Office.Interop.excel的情况下导出tp excel?如果没有,请先检查文件是否已创建,然后使用`If System.IO.Directory.Exists(tempPath)创建=False然后是System.IO.Directory.CreateDirectory(tempPath),如果System.IO.File.Exists(strFileName)=False然后是System.IO.File.Create(strFileName)如果End If End If`其中strFileName是文件名,tempPath只是excel文件所在的文件夹,是否可以在不使用Microsoft.Office.Interop.excel的情况下导出tp excel?