DataGridエクスポートへのエクスポート -- vb.net フィールド と datagrid フィールド と .net-1.1 フィールド 関連 問題

DataGrid export to excel












0
vote

問題

日本語

<コード> data se tを埋める DataGrid control があります。

DataGridコントロールでデータ・セットのすべてのフィールドを表示しません。

excel file から excel file を作成したい。

解決方法を取得する方法?

(Windowsフォーム、<コード> vb net 1.1 )

英語

I have a DataGrid control that fills with a data set.

I don't show all fields of data set in DataGrid control.

I want to create an excel file from my DataGrid.

How to get solution?

(windows form , vb net 1.1)

</div
        
 
 

回答リスト

1
 
vote

これを試してみてください link

または

これを試してください <事前> <コード> 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()

 

Try this Link

OR

Try This

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() 
</div
 
 
   
   
1
 
vote
<事前> <コード> Private Sub btnExportToExcel_Click(sender As Object, e As EventArgs) Handles btnExportToExcel.Click Dim xlApp As Excel.Application = New Excel.Application xlApp.SheetsInNewWorkbook = 1 Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Worksheets.Item(1) xlWorkSheet.Name = "Example_Export" For nRow = 0 To dgvDataToExport.Rows.Count - 1 For nCol = 0 To dgvDataToExport.Columns.Count - 1 xlWorkSheet.Cells(nRow + 1, nCol + 1) = dgvDataToExport.Rows(nRow).Cells(nCol).Value Next nCol Next nRow xlApp.DisplayAlerts = False xlWorkBook.SaveAs("C:Example.xlsx", Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _ Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges) xlWorkBook.Close() xlApp.Quit() End Sub
 
Private Sub btnExportToExcel_Click(sender As Object, e As EventArgs) Handles btnExportToExcel.Click          Dim xlApp As Excel.Application = New Excel.Application          xlApp.SheetsInNewWorkbook = 1          Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add         Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Worksheets.Item(1)          xlWorkSheet.Name = "Example_Export"          For nRow = 0 To dgvDataToExport.Rows.Count - 1              For nCol = 0 To dgvDataToExport.Columns.Count - 1                 xlWorkSheet.Cells(nRow + 1, nCol + 1) = dgvDataToExport.Rows(nRow).Cells(nCol).Value             Next nCol          Next nRow          xlApp.DisplayAlerts = False          xlWorkBook.SaveAs("C:Example.xlsx", Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _                            Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges)          xlWorkBook.Close()         xlApp.Quit()      End Sub 
</div
 
 
1
 
vote

これを試してみてください:

<事前> <コード> 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
 

Try with this one:

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 
</div
 
 
0
 
vote

このプリンシップワークに多分。

<事前> <コード> 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(); }
 

Maybe in this princip work.

  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();              } 
</div
 
 
   
   
0
 
vote

を試してください <事前> <コード> If Not dgv.RowCount = 0 Then Dim folderBrowser As New FolderBrowserDialog folderBrowser.Description = "Select location to save the report" Dim filepath1 As String = "" If (folderBrowser.ShowDialog() = DialogResult.OK) Then filepath1 = folderBrowser.SelectedPath Else Exit Sub End If Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet Try Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("sheet1") '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim titleStyle As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle1") titleStyle.Font.Bold = True titleStyle.Font.Size = "18" titleStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red) xlWorkSheet.Cells(2, 2) = "Employee Payment Report" xlWorkSheet.Cells(2, 4) = DateAndTime.Now.ToString("dd/MM/yyyy") xlWorkSheet.Cells(2, 2).Style = "NewStyle1" xlWorkSheet.Cells(2, 4).Style = "NewStyle1" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '====================================================================================================== Dim headerStyle As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle") headerStyle.Font.Bold = True headerStyle.Font.Size = "12" headerStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Brown) For k = 1 To dgv.Columns.Count xlWorkSheet.Cells(4, k) = dgv.Columns(k - 1).HeaderText xlWorkSheet.Cells(4, k).Style = "NewStyle" Next '======================================================================================================= Dim str As String = "" Dim l As Integer = 1 j = 6 Dim amt As Double = 0.0 For i = 0 To dgv.RowCount - 1 amt = amt + dgv.Rows(i).Cells(4).Value For m = 0 To dgv.ColumnCount - 1 xlWorkSheet.Cells(j, l) = dgv(m, i).Value.ToString() str = dgv(m, i).Value.ToString() l = l + 1 Next j = j + 1 l = 1 Next '====================================================================================================== Dim lastStyle As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle2") lastStyle.Font.Bold = True lastStyle.Font.Size = "12" lastStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue) Dim c As Integer = dgv.ColumnCount xlWorkSheet.Cells(j + 2, c - 1) = "Total Amount" xlWorkSheet.Cells(j + 2, c) = amt.ToString xlWorkSheet.Cells(j + 2, c - 1).Style = "NewStyle2" xlWorkSheet.Cells(j + 2, c).Style = "NewStyle2" '======================================================================================================= xlWorkSheet.SaveAs(filepath1 + "EmployeePaymentReport.xlsx") xlWorkBook.Close() xlApp.Quit() cls.releaseObject(xlApp) cls.releaseObject(xlWorkBook) cls.releaseObject(xlWorkSheet) MsgBox("You can find the file at " + filepath1 + "EmployeePaymentReport.xlsx") Catch ex As Exception MsgBox(ex.Message) For Each Process In System.Diagnostics.Process.GetProcessesByName("EXCEL") If Process.MainModule.ModuleName.ToUpper().Equals("EXCEL.EXE") Then Process.Kill() End If Next End Try Else Exit Sub End If Catch ex As Exception End Try End Sub
 

Try

        If Not dgv.RowCount = 0 Then             Dim folderBrowser As New FolderBrowserDialog             folderBrowser.Description = "Select location to save the report"             Dim filepath1 As String = ""             If (folderBrowser.ShowDialog() = DialogResult.OK) Then                 filepath1 = folderBrowser.SelectedPath             Else                 Exit Sub             End If              Dim xlApp As Microsoft.Office.Interop.Excel.Application             Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook             Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet             Try                  Dim misValue As Object = System.Reflection.Missing.Value                 Dim i As Integer                 Dim j As Integer                 xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass                 xlWorkBook = xlApp.Workbooks.Add(misValue)                 xlWorkSheet = xlWorkBook.Sheets("sheet1")                 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''                 Dim titleStyle As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle1")                 titleStyle.Font.Bold = True                 titleStyle.Font.Size = "18"                 titleStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)                  xlWorkSheet.Cells(2, 2) = "Employee Payment Report"                 xlWorkSheet.Cells(2, 4) = DateAndTime.Now.ToString("dd/MM/yyyy")                 xlWorkSheet.Cells(2, 2).Style = "NewStyle1"                 xlWorkSheet.Cells(2, 4).Style = "NewStyle1"                 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''                  '======================================================================================================                 Dim headerStyle As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle")                 headerStyle.Font.Bold = True                 headerStyle.Font.Size = "12"                 headerStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Brown)                  For k = 1 To dgv.Columns.Count                     xlWorkSheet.Cells(4, k) = dgv.Columns(k - 1).HeaderText                     xlWorkSheet.Cells(4, k).Style = "NewStyle"                  Next                 '=======================================================================================================                 Dim str As String = ""                 Dim l As Integer = 1                 j = 6                 Dim amt As Double = 0.0                 For i = 0 To dgv.RowCount - 1                     amt = amt + dgv.Rows(i).Cells(4).Value                     For m = 0 To dgv.ColumnCount - 1                         xlWorkSheet.Cells(j, l) = dgv(m, i).Value.ToString()                         str = dgv(m, i).Value.ToString()                         l = l + 1                     Next                     j = j + 1                     l = 1                 Next                 '======================================================================================================                 Dim lastStyle As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle2")                 lastStyle.Font.Bold = True                 lastStyle.Font.Size = "12"                 lastStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue)                 Dim c As Integer = dgv.ColumnCount                 xlWorkSheet.Cells(j + 2, c - 1) = "Total Amount"                 xlWorkSheet.Cells(j + 2, c) = amt.ToString                 xlWorkSheet.Cells(j + 2, c - 1).Style = "NewStyle2"                 xlWorkSheet.Cells(j + 2, c).Style = "NewStyle2"                 '=======================================================================================================                 xlWorkSheet.SaveAs(filepath1 + "EmployeePaymentReport.xlsx")                 xlWorkBook.Close()                 xlApp.Quit()                  cls.releaseObject(xlApp)                 cls.releaseObject(xlWorkBook)                 cls.releaseObject(xlWorkSheet)                  MsgBox("You can find the file at " + filepath1 + "EmployeePaymentReport.xlsx")             Catch ex As Exception                 MsgBox(ex.Message)                 For Each Process In System.Diagnostics.Process.GetProcessesByName("EXCEL")                     If Process.MainModule.ModuleName.ToUpper().Equals("EXCEL.EXE") Then                         Process.Kill()                     End If                 Next             End Try         Else             Exit Sub         End If     Catch ex As Exception      End Try End Sub 
</div
 
 

関連する質問

6  例外でDataReaderを閉じる方法  ( How to close a datareader on exception ) 
私のデータ層のいくつかの方法で次のコードを持っています: <事前> <コード> StringBuilder sb = new StringBuilder(); SqlCommand s = new SqlCommand(sb.ToString(), conn)...

3  デフォルトのアプリケーションプール設定プロパティをプログラムで変更する方法  ( How do i change default application pool setting properties programatically in ) 
C#を使用して、IIS 6.0のデフォルトのアプリケーションプールで一部のプロパティを変更し、.NET 1.1フレームワークを使用してコードを使用するように制約されています。私は.NET 1.1 COZに制限されています.NET 1.1を使用するWebサイト...

0  NET 1.1のXMLTextReader  ( Xmltextreader in net 1 1 ) 
XMLファイルを読み取るプロセスがあります。 XMLTextReaderクラスを使用してこれを行い、XML Parser / Readerのみが高速であるとされると考えられます。 は、1メガバイトテストファイルですぐに機能しますが、ライブシステムで12のME...

5  ASP.NET 1.1 Webアプリケーションから.NET 2.0 COMオブジェクトを呼び出すことができますか?  ( Can you call a net 2 0 com object from an asp net 1 1 web app ) 
ASP.NET 1.1 Webアプリケーションを持っています。これはC ++ COMオブジェクトを呼び出し、それが順番に.NET COMオブジェクトを呼び出します。元々、この.NET COMオブジェクトは.NET 1.1アセンブリでした。さて、この.NET ...

0  UpdatePanelアップグレードされたWebProjectで機能していません  ( Updatepanel not working in upgraded webproject ) 
私はもともとvb.net 1.1で開発された古いWebアプリケーションを使用しています。アプリケーションは4.0フレームワークに変換され、AjaxとUpdatePanelを使用したいと思いました。ただし、UpdatePanelが期待どおりに機能することはできま...

4  WebServicesでカスタムオブジェクトを返す方法  ( How to return custom objects with webservices ) 
[問題] サーバーマシンとクライアントマシンにデプロイされる共有ライブラリを作成したことは、ライブラリから提供されたクラスとクライアントサーバー間でどのように通信するのですか? Webサービスによって返されたシリアル化されたオブジェクトが共有ライブラ...

3  絵の箱は空白になります  ( Picture box goes blank ) 
VisualBasic.net映像ボックスを有効にしてから描画すると、それは描画してからすぐに空白になります。それを再描画するとうまくいきますが、私がそれを描く初めて常に常にめちゃくちゃになります。これはいくつかの異なるプログラムで発生し、ヘルプファイルはヘ...

1  GetProcessesByName()およびWindows Server 2003スケジュールされたタスク  ( Getprocessesbyname and windows server 2003 scheduled task ) 
Windows Server 2003のスケジュールされたタスクとして正常に実行する必要があるのは、次のコードに必要なユーザー権限が必要なのかを知っています。 <事前> <コード> System.Diagnostics.Process.GetProcesse...

0  Page.toString().NET 1.1で少し奇妙な動作をしますか?  ( Page tostring behaves a bit weird in net 1 1 ) 
私はどのページのどちらのページをチェックする必要があるコントロールがありますので、それに応じて特定の変数を設定できます。 <事前> <コード> string pageName = this.Page.ToString(); switch (pageName) ...

0  DataGrid 1.0から行順を取得します  ( Retrieve row order from datagrid 1 0 ) 
私は.NET 1.1であるレガシアプリを作業しています。(列ヘッダーをクリックして)DataGrid Post Sortの行を取得する必要があります。これは簡単な作業のように聞こえましたが、その答えは私をもらうようです。私はデータセットからの行を元に戻すことが...




© 2022 cndgn.com All Rights Reserved. Q&Aハウス 全著作権所有