怎么把datagridview 里的数据导出到一个excel里?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么把datagridview 里的数据导出到一个excel里?相关的知识,希望对你有一定的参考价值。

从网上找个很多代码结果T-M-D却是不能运行的!
请各位把自己的代码贴上,好的我一次给30分!
补充一下是 vb.net winForm程序
这个问题就这么难吗?
有没有高手了!
我的email :hhayyok@163.com

继续补充问题!
二楼的兄弟回答的有些问题,好像是 vs2003的,但是在2005中就会有很多问题。大家需要针对2005来回答好吗?

参考技术A 参考例子(根据你的实际要求修改下就行了):
Private ExcelApp As Microsoft.Office.Interop.Excel.Application
Private ExcelBook As Microsoft.Office.Interop.Excel.Workbook
Private ExcelSheet As Microsoft.Office.Interop.Excel.Worksheet
private sub EXCELEX()
Dim m_bWithoutDefultSheet As Boolean = False
ExcelApp = CreateObject("Excel.application")
ExcelApp.Visible = False

If ExcelApp.Workbooks.Count = 0 Then '创建表格
ExcelBook = ExcelApp.Workbooks.Add
End If

For Each ExcelSheet In ExcelBook.Worksheets
If ExcelBook.Worksheets.Count > 1 Then
m_bWithoutDefultSheet = False

If Mid(ExcelSheet.Name, 1, 5) = "Sheet" Then
ExcelSheet.Delete()
m_bWithoutDefultSheet = True
End If
Else

If Mid(ExcelSheet.Name, 1, 5) <> "Sheet" Then
ExcelSheet = ExcelBook.Worksheets.Add()
End If
ExcelSheet.Name = "111" m_bWithoutDefultSheet = True
End If
Next

If Not m_bWithoutDefultSheet Then
ExcelSheet = ExcelBook.Worksheets.Add()
ExcelSheet.Name = "111"
End If

Call SetPageMargin()

ExcelSheet.Range("A:A").ColumnWidth = 40 '写内容
ExcelSheet.Range("B:B").ColumnWidth = 20
ExcelSheet.Range("C:C").ColumnWidth = 10
ExcelSheet.Range("D:D").ColumnWidth = 10

ExcelSheet.Range("A1:D1").MergeCells = True
ExcelSheet.Range("A1:D1").Font.Name = "黑体"
ExcelSheet.Range("A1:D1").Font.Size = 16
ExcelSheet.Range("A1:D1").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
If textbox1.Text <> "全部" And textbox1.Text <> "" Then
ExcelSheet.Cells(1, 1) = "********"
Else
ExcelSheet.Cells(1, 1) = "9999999"
End If

ExcelSheet.PageSetup.PaperSize = 9
ExcelSheet.Range("A2:D2").Font.Name = "宋体"
ExcelSheet.Range("A2:D2").Font.Size = 12
ExcelSheet.Range("A2:D2").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
ExcelSheet.Cells(2, 1) = "22"
ExcelSheet.Cells(2, 2) = "33"
ExcelSheet.Cells(2, 3) = "44"
ExcelSheet.Cells(2, 4) = "55"

SetCellStyle("A3:D12" , 18)

For i As Integer = 0 To 9

ExcelSheet.Cells(i + 3, 1) = "11"
ExcelSheet.Cells(i + 3, 2) = "22"
ExcelSheet.Cells(i + 3, 3) ="33"
ExcelSheet.Cells(i + 3, 4) ="44"
Next

ShowGird()
ExcelApp.Visible = True
ExcelSheet.PrintPreview()
ExcelApp.Visible = False
ExcelApp.DisplayAlerts = False '不提示保存
ExcelBook.Close(False)
ExcelApp.Quit()
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
GC.Collect()
end sub

Private Sub SetPageMargin()
ExcelSheet.PageSetup.LeftMargin = ExcelApp.InchesToPoints(0.748031496062992)
ExcelSheet.PageSetup.RightMargin = ExcelApp.InchesToPoints(0.511811023622047) '1.3
ExcelSheet.PageSetup.BottomMargin = ExcelApp.InchesToPoints(0.78740157480315) '2.0
ExcelSheet.PageSetup.TopMargin = ExcelApp.InchesToPoints(0.984251968503937) '2.5
ExcelSheet.PageSetup.HeaderMargin = ExcelApp.InchesToPoints(0.511811023622047) '1.3
ExcelSheet.PageSetup.FooterMargin = ExcelApp.InchesToPoints(0.511811023622047) '1.3
End Sub

Private Sub SetCellStyle(ByVal Range As String, ByVal RowHeight As Decimal)
With ExcelSheet.Range(Range)
.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
.NumberFormatLocal = "@"
.RowHeight = RowHeight
.Font.Name = "宋体"
.Font.Size = 12
End With
End Sub

Public Sub ShowGird()
With ExcelSheet.Range("A2:D12" )
With .Borders(7)
.LineStyle = 1
End With
With .Borders(10)
.LineStyle = 1
End With
With .Borders(8)
.LineStyle = 1
End With
With .Borders(9)
.LineStyle = 1
End With
With .Borders(12)
.LineStyle = 1
End With
With .Borders(11)
.LineStyle = 1
End With
End With

End Sub本回答被提问者采纳
参考技术B using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms ;
using System.IO;

namespace OutputToExecl

public class ExportXLS

public ExportXLS()



public static void ExportDataGridViewToExcel(DataGridView dataGridview1)

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出Excel文件到";

DateTime now = DateTime.now;
saveFileDialog.FileName = now.Year.ToString().PadLeft(2)
+now.Month.ToString().PadLeft(2,'0')
+now.Day.ToString().PadLeft(2,'0')+"-"
+now.Hour.ToString().PadLeft(2,'0')
+now.Minute.ToString().PadLeft(2,'0')
+now.Second.ToString().PadLeft(2,'0');

saveFileDialog.ShowDialog();

Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
try

//写标题
for (int i = 0; i < dataGridview1.ColumnCount; i++)

if (i > 0)

str += "\t";

str += dataGridview1.Columns[i].HeaderText;


sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridview1.Rows.Count; j++)

string tempStr = "";
for (int k = 0; k < dataGridview1.Columns.Count; k++)

if (k > 0)

tempStr += "\t";

tempStr += dataGridview1.Rows[j].Cells[k].Value.ToString();

sw.WriteLine(tempStr);

sw.Close();
myStream.Close();

catch (Exception e)

MessageBox.Show(e.ToString());

finally

sw.Close();
myStream.Close();




怎样把dataGridView的内容导入数据库

参考技术A 改成string a = this.dataGridView1.Rows[0].Cells[0].Value.ToString();追问:就和你添加是一样的,SQL语句改成UPDATE就行了追问:dataGridView 有个选中的功能,可以指定修改选中的行补充:前提是你这行数据要存在唯一字段追问:我们有个“修改”按钮,通过点击“修改”按钮,然后修改dataGridView中的数据的 不是直接在里面修改回答:你前面已经说了是修改选中行的数据,既然是选中行的话,可以取到选中行的行数,然后取到这行数据在数据库存在的唯一字段,然后执行修改。至于你修改方式,因为看不到你界面,所以过程不好说,给你大概的步骤1:获取选中行的行数3:执行修改

以上是关于怎么把datagridview 里的数据导出到一个excel里?的主要内容,如果未能解决你的问题,请参考以下文章

在C#中,如何把DataGridview中的数据导出到一个Excel表中

怎么把Listview的值传到DataGridView里面

VB.net 中datagridview 怎么从数据库里取出数据

C#winform中如何把表导出到EXCEL

在C#,winform下怎样实现使用打印控件printDocument来打印datagridview里的内容?

c# 怎么把dataGridView的数据 用Excel导出! 详细点啊