如何将DataSet中的某些记录导出到excel?搜索了以往的提问找不到合适的,麻烦高手详细指点一下或给个例子,多谢了!
24 个解决方案
#1
搜索符合条件的填充一个新的datatable然后导出
#2
就是不会导出到excel啊!能给个代码吗?
#3
再起来!
#4
我也不知道怎么做,帮顶
#5
自己写比较复杂涉及COM调用和格式控制
简单的可以用Reporting Service或水晶报表
简单的可以用Reporting Service或水晶报表
#6
详细点可以吗?
#7
VB。NET 和C# 基本上是差不多的 简单的修改一下应该能实现的!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mydataset As New DataSet
Dim myadapter As OleDbDataAdapter
Dim myconnection As New OleDbConnection
Dim str As String = Nothing
Dim Table As New System.Data.DataTable
myconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\1dbTT.mdb"
myconnection.Open()
str = "select * from tab_DATA_Send"
myadapter = New OleDbDataAdapter(str, myconnection)
myadapter.Fill(mydataset, "queueDateTime")
Table = mydataset.Tables("queueDateTime")
Dim total_col As Integer = Table.Columns.Count
Dim total_row As Integer = Table.Rows.Count
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet
Try
If total_col < 1 Or total_row < 1 Then
MsgBox("没有可供导入的数据!", MsgBoxStyle.Information, "系统提示")
Exit Sub
End If
GC.Collect()
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
Dim Col As Integer
Dim Row As Integer
Dim st_row As Integer = 1 ''数据列头开始行,(列头)
Dim trueCol As Integer = Table.Columns.Count
Dim HeaderArray(0, trueCol) As Object '定义一个存放表的各个字段的数组
Dim DataArray(total_row - 1, trueCol) As Object '定义一个存放表中除字段以外的内容的数组
For Col = 0 To total_col - 1
HeaderArray(0, Col) = Table.Columns(Col).ColumnName
Next
xlSheet.Range("A" & st_row).Resize(st_row, trueCol).Value = HeaderArray '存放到EXCEL中间从第一行开始
For Row = 0 To total_row - 1
For Col = 0 To total_col - 1
DataArray(Row, Col) = Table.Rows(Row).Item(Col)
Next
Next
xlSheet.Range("A" & st_row + 1).Resize(total_row, trueCol).Value = DataArray
Catch ex As Exception
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
xlBook.Close()
xlBook = Nothing
xlApp.Quit()
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
MsgBox(ex.ToString)
Exit Sub
Finally
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
'' xlBook.Close() '退出EXCEL 小界面
xlBook = Nothing
' 'xlApp.Quit() ‘退出EXCEL 窗体
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mydataset As New DataSet
Dim myadapter As OleDbDataAdapter
Dim myconnection As New OleDbConnection
Dim str As String = Nothing
Dim Table As New System.Data.DataTable
myconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\1dbTT.mdb"
myconnection.Open()
str = "select * from tab_DATA_Send"
myadapter = New OleDbDataAdapter(str, myconnection)
myadapter.Fill(mydataset, "queueDateTime")
Table = mydataset.Tables("queueDateTime")
Dim total_col As Integer = Table.Columns.Count
Dim total_row As Integer = Table.Rows.Count
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet
Try
If total_col < 1 Or total_row < 1 Then
MsgBox("没有可供导入的数据!", MsgBoxStyle.Information, "系统提示")
Exit Sub
End If
GC.Collect()
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
Dim Col As Integer
Dim Row As Integer
Dim st_row As Integer = 1 ''数据列头开始行,(列头)
Dim trueCol As Integer = Table.Columns.Count
Dim HeaderArray(0, trueCol) As Object '定义一个存放表的各个字段的数组
Dim DataArray(total_row - 1, trueCol) As Object '定义一个存放表中除字段以外的内容的数组
For Col = 0 To total_col - 1
HeaderArray(0, Col) = Table.Columns(Col).ColumnName
Next
xlSheet.Range("A" & st_row).Resize(st_row, trueCol).Value = HeaderArray '存放到EXCEL中间从第一行开始
For Row = 0 To total_row - 1
For Col = 0 To total_col - 1
DataArray(Row, Col) = Table.Rows(Row).Item(Col)
Next
Next
xlSheet.Range("A" & st_row + 1).Resize(total_row, trueCol).Value = DataArray
Catch ex As Exception
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
xlBook.Close()
xlBook = Nothing
xlApp.Quit()
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
MsgBox(ex.ToString)
Exit Sub
Finally
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
'' xlBook.Close() '退出EXCEL 小界面
xlBook = Nothing
' 'xlApp.Quit() ‘退出EXCEL 窗体
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
End Try
End Sub
#8
我不懂VB,请问 GC.Collect()是什么啊?没看到定义啊!
#9
Excel.Application excel1=new Excel.Application();
excel1.Application.Workbooks.Add(true);
int index=0;
foreach(DataColumn col in dt.Columns)
{
index++;
excel1.Cells[1,index]=col.ColumnName;
}
int rowindex=1;
foreach(DataRow dr in dt.Rows)
{
rowindex++;
index=0;
foreach(DataColumn co in dt.Columns)
{
index++;
excel1.Cells[rowindex,index]=dr[co.ColumnName].ToString();
}
}
excel1.Visible=true;
}
excel1.Application.Workbooks.Add(true);
int index=0;
foreach(DataColumn col in dt.Columns)
{
index++;
excel1.Cells[1,index]=col.ColumnName;
}
int rowindex=1;
foreach(DataRow dr in dt.Rows)
{
rowindex++;
index=0;
foreach(DataColumn co in dt.Columns)
{
index++;
excel1.Cells[rowindex,index]=dr[co.ColumnName].ToString();
}
}
excel1.Visible=true;
}
#10
你可以考虑将EXCEL当作一个数据库,然后进行数据导入
#11
楼上的清风sky大哥,"Excel"怎么来的啊?没有命名空间啊!在此谢过了!
#12
To:yycpp1
不明白你的意思!我是要将数据导出为Excel啊,也就是最后变成一个.xls文件
不明白你的意思!我是要将数据导出为Excel啊,也就是最后变成一个.xls文件
#13
楼上的清风sky大哥,"Excel"怎么来的啊?没有命名空间啊!在此谢过了!
----------------------------------------------------------------
添加引用-->COM-->Microsoft Excel 9.0 Object Library
----------------------------------------------------------------
添加引用-->COM-->Microsoft Excel 9.0 Object Library
#14
多谢多谢!试试就结贴!
#15
If you are just exporting simple table structure, just save the dataset as a comma (,) separated file with extension .csv.
Excel can directly open csv files.
Excel can directly open csv files.
#16
真羞人,看不懂楼上的英语,那位翻译一下!
#17
private void Button2_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-excel", "供应商信息.xls"); //导到Excel
}
private void ExportDataGrid(string FileType, string FileName) //导出
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
{
ExportDataGrid("application/ms-excel", "供应商信息.xls"); //导到Excel
}
private void ExportDataGrid(string FileType, string FileName) //导出
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
#18
多谢各位的回答,
mingwj1980() 的方法可能我的DataGrid里面放了CheckBox的缘故,并不能输出.对于此方法,我看不懂! 错误提示"类型“DataGridLinkButton”的控件“dg_ghs__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。"很有意思的是刚好我要导出信息的也是如你代码中的一样,也是"供货商信息",我的只是自己做着练手而已!
losky(清风sky)和chendazhi(不务正业)提到的方法呢好象是在服务端生成.xls文件,问题就是老是提示"拒绝访问",可我已经把相关的文件夹中的安全属性都改为Everyone写入了.
莫非非用第三方控件不可啊?
mingwj1980() 的方法可能我的DataGrid里面放了CheckBox的缘故,并不能输出.对于此方法,我看不懂! 错误提示"类型“DataGridLinkButton”的控件“dg_ghs__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。"很有意思的是刚好我要导出信息的也是如你代码中的一样,也是"供货商信息",我的只是自己做着练手而已!
losky(清风sky)和chendazhi(不务正业)提到的方法呢好象是在服务端生成.xls文件,问题就是老是提示"拒绝访问",可我已经把相关的文件夹中的安全属性都改为Everyone写入了.
莫非非用第三方控件不可啊?
#19
mark
#20
给你一个代码你自己看吧,过程是先编辑好要导出的datatable或者dataview,然后直接生成Excel文件;
//输出到Excel
public void ExportExcel(DataView dv)
{
SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls" ;
saveFileDialog1.FilterIndex = 0 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CreatePrompt=true;
saveFileDialog1.Title="导出Excel文件到";
DateTime now=DateTime.Now;
saveFileDialog1.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');
saveFileDialog1.ShowDialog();
Stream myStream;
myStream=saveFileDialog1.OpenFile();
StreamWriter sw=new StreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312"));
String str="";
//写标题
for(int i=0;i<dv.Table.Columns.Count;i++)
{
if(i>0)
{
str+="\t";
}
str+=dv.Table.Columns[i].ColumnName;
}
sw.WriteLine(str);
//写内容
for(int rowNo=0;rowNo<dv.Count;rowNo++)
{
String tempstr="";
for(int columnNo=0;columnNo<dv.Table.Columns.Count;columnNo++)
{
if(columnNo>0)
{
tempstr+="\t";
}
//tempstr+=dg.Rows[rowNo,columnNo].ToString();
tempstr+=dv.Table.Rows[rowNo][columnNo].ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
}
//输出到Excel
public void ExportExcel(DataView dv)
{
SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls" ;
saveFileDialog1.FilterIndex = 0 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CreatePrompt=true;
saveFileDialog1.Title="导出Excel文件到";
DateTime now=DateTime.Now;
saveFileDialog1.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');
saveFileDialog1.ShowDialog();
Stream myStream;
myStream=saveFileDialog1.OpenFile();
StreamWriter sw=new StreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312"));
String str="";
//写标题
for(int i=0;i<dv.Table.Columns.Count;i++)
{
if(i>0)
{
str+="\t";
}
str+=dv.Table.Columns[i].ColumnName;
}
sw.WriteLine(str);
//写内容
for(int rowNo=0;rowNo<dv.Count;rowNo++)
{
String tempstr="";
for(int columnNo=0;columnNo<dv.Table.Columns.Count;columnNo++)
{
if(columnNo>0)
{
tempstr+="\t";
}
//tempstr+=dg.Rows[rowNo,columnNo].ToString();
tempstr+=dv.Table.Rows[rowNo][columnNo].ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
}
#21
http://linfuguo.cnblogs.com/archive/2006/03/23/357155.html
我的实现,VS.net2003下已经测试通过
我的实现,VS.net2003下已经测试通过
#22
多谢大家的回答,这几天一直没有时间弄,先结了贴吧!
#23
mark
#24
数据超过65536怎么办?
#1
搜索符合条件的填充一个新的datatable然后导出
#2
就是不会导出到excel啊!能给个代码吗?
#3
再起来!
#4
我也不知道怎么做,帮顶
#5
自己写比较复杂涉及COM调用和格式控制
简单的可以用Reporting Service或水晶报表
简单的可以用Reporting Service或水晶报表
#6
详细点可以吗?
#7
VB。NET 和C# 基本上是差不多的 简单的修改一下应该能实现的!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mydataset As New DataSet
Dim myadapter As OleDbDataAdapter
Dim myconnection As New OleDbConnection
Dim str As String = Nothing
Dim Table As New System.Data.DataTable
myconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\1dbTT.mdb"
myconnection.Open()
str = "select * from tab_DATA_Send"
myadapter = New OleDbDataAdapter(str, myconnection)
myadapter.Fill(mydataset, "queueDateTime")
Table = mydataset.Tables("queueDateTime")
Dim total_col As Integer = Table.Columns.Count
Dim total_row As Integer = Table.Rows.Count
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet
Try
If total_col < 1 Or total_row < 1 Then
MsgBox("没有可供导入的数据!", MsgBoxStyle.Information, "系统提示")
Exit Sub
End If
GC.Collect()
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
Dim Col As Integer
Dim Row As Integer
Dim st_row As Integer = 1 ''数据列头开始行,(列头)
Dim trueCol As Integer = Table.Columns.Count
Dim HeaderArray(0, trueCol) As Object '定义一个存放表的各个字段的数组
Dim DataArray(total_row - 1, trueCol) As Object '定义一个存放表中除字段以外的内容的数组
For Col = 0 To total_col - 1
HeaderArray(0, Col) = Table.Columns(Col).ColumnName
Next
xlSheet.Range("A" & st_row).Resize(st_row, trueCol).Value = HeaderArray '存放到EXCEL中间从第一行开始
For Row = 0 To total_row - 1
For Col = 0 To total_col - 1
DataArray(Row, Col) = Table.Rows(Row).Item(Col)
Next
Next
xlSheet.Range("A" & st_row + 1).Resize(total_row, trueCol).Value = DataArray
Catch ex As Exception
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
xlBook.Close()
xlBook = Nothing
xlApp.Quit()
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
MsgBox(ex.ToString)
Exit Sub
Finally
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
'' xlBook.Close() '退出EXCEL 小界面
xlBook = Nothing
' 'xlApp.Quit() ‘退出EXCEL 窗体
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mydataset As New DataSet
Dim myadapter As OleDbDataAdapter
Dim myconnection As New OleDbConnection
Dim str As String = Nothing
Dim Table As New System.Data.DataTable
myconnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\1dbTT.mdb"
myconnection.Open()
str = "select * from tab_DATA_Send"
myadapter = New OleDbDataAdapter(str, myconnection)
myadapter.Fill(mydataset, "queueDateTime")
Table = mydataset.Tables("queueDateTime")
Dim total_col As Integer = Table.Columns.Count
Dim total_row As Integer = Table.Rows.Count
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet
Try
If total_col < 1 Or total_row < 1 Then
MsgBox("没有可供导入的数据!", MsgBoxStyle.Information, "系统提示")
Exit Sub
End If
GC.Collect()
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
Dim Col As Integer
Dim Row As Integer
Dim st_row As Integer = 1 ''数据列头开始行,(列头)
Dim trueCol As Integer = Table.Columns.Count
Dim HeaderArray(0, trueCol) As Object '定义一个存放表的各个字段的数组
Dim DataArray(total_row - 1, trueCol) As Object '定义一个存放表中除字段以外的内容的数组
For Col = 0 To total_col - 1
HeaderArray(0, Col) = Table.Columns(Col).ColumnName
Next
xlSheet.Range("A" & st_row).Resize(st_row, trueCol).Value = HeaderArray '存放到EXCEL中间从第一行开始
For Row = 0 To total_row - 1
For Col = 0 To total_col - 1
DataArray(Row, Col) = Table.Rows(Row).Item(Col)
Next
Next
xlSheet.Range("A" & st_row + 1).Resize(total_row, trueCol).Value = DataArray
Catch ex As Exception
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
xlBook.Close()
xlBook = Nothing
xlApp.Quit()
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
MsgBox(ex.ToString)
Exit Sub
Finally
xlSheet = Nothing
xlApp.DisplayAlerts = False
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoClose)
'' xlBook.Close() '退出EXCEL 小界面
xlBook = Nothing
' 'xlApp.Quit() ‘退出EXCEL 窗体
xlApp.DisplayAlerts = True
xlApp = Nothing
GC.Collect()
End Try
End Sub
#8
我不懂VB,请问 GC.Collect()是什么啊?没看到定义啊!
#9
Excel.Application excel1=new Excel.Application();
excel1.Application.Workbooks.Add(true);
int index=0;
foreach(DataColumn col in dt.Columns)
{
index++;
excel1.Cells[1,index]=col.ColumnName;
}
int rowindex=1;
foreach(DataRow dr in dt.Rows)
{
rowindex++;
index=0;
foreach(DataColumn co in dt.Columns)
{
index++;
excel1.Cells[rowindex,index]=dr[co.ColumnName].ToString();
}
}
excel1.Visible=true;
}
excel1.Application.Workbooks.Add(true);
int index=0;
foreach(DataColumn col in dt.Columns)
{
index++;
excel1.Cells[1,index]=col.ColumnName;
}
int rowindex=1;
foreach(DataRow dr in dt.Rows)
{
rowindex++;
index=0;
foreach(DataColumn co in dt.Columns)
{
index++;
excel1.Cells[rowindex,index]=dr[co.ColumnName].ToString();
}
}
excel1.Visible=true;
}
#10
你可以考虑将EXCEL当作一个数据库,然后进行数据导入
#11
楼上的清风sky大哥,"Excel"怎么来的啊?没有命名空间啊!在此谢过了!
#12
To:yycpp1
不明白你的意思!我是要将数据导出为Excel啊,也就是最后变成一个.xls文件
不明白你的意思!我是要将数据导出为Excel啊,也就是最后变成一个.xls文件
#13
楼上的清风sky大哥,"Excel"怎么来的啊?没有命名空间啊!在此谢过了!
----------------------------------------------------------------
添加引用-->COM-->Microsoft Excel 9.0 Object Library
----------------------------------------------------------------
添加引用-->COM-->Microsoft Excel 9.0 Object Library
#14
多谢多谢!试试就结贴!
#15
If you are just exporting simple table structure, just save the dataset as a comma (,) separated file with extension .csv.
Excel can directly open csv files.
Excel can directly open csv files.
#16
真羞人,看不懂楼上的英语,那位翻译一下!
#17
private void Button2_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-excel", "供应商信息.xls"); //导到Excel
}
private void ExportDataGrid(string FileType, string FileName) //导出
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
{
ExportDataGrid("application/ms-excel", "供应商信息.xls"); //导到Excel
}
private void ExportDataGrid(string FileType, string FileName) //导出
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
#18
多谢各位的回答,
mingwj1980() 的方法可能我的DataGrid里面放了CheckBox的缘故,并不能输出.对于此方法,我看不懂! 错误提示"类型“DataGridLinkButton”的控件“dg_ghs__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。"很有意思的是刚好我要导出信息的也是如你代码中的一样,也是"供货商信息",我的只是自己做着练手而已!
losky(清风sky)和chendazhi(不务正业)提到的方法呢好象是在服务端生成.xls文件,问题就是老是提示"拒绝访问",可我已经把相关的文件夹中的安全属性都改为Everyone写入了.
莫非非用第三方控件不可啊?
mingwj1980() 的方法可能我的DataGrid里面放了CheckBox的缘故,并不能输出.对于此方法,我看不懂! 错误提示"类型“DataGridLinkButton”的控件“dg_ghs__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。"很有意思的是刚好我要导出信息的也是如你代码中的一样,也是"供货商信息",我的只是自己做着练手而已!
losky(清风sky)和chendazhi(不务正业)提到的方法呢好象是在服务端生成.xls文件,问题就是老是提示"拒绝访问",可我已经把相关的文件夹中的安全属性都改为Everyone写入了.
莫非非用第三方控件不可啊?
#19
mark
#20
给你一个代码你自己看吧,过程是先编辑好要导出的datatable或者dataview,然后直接生成Excel文件;
//输出到Excel
public void ExportExcel(DataView dv)
{
SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls" ;
saveFileDialog1.FilterIndex = 0 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CreatePrompt=true;
saveFileDialog1.Title="导出Excel文件到";
DateTime now=DateTime.Now;
saveFileDialog1.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');
saveFileDialog1.ShowDialog();
Stream myStream;
myStream=saveFileDialog1.OpenFile();
StreamWriter sw=new StreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312"));
String str="";
//写标题
for(int i=0;i<dv.Table.Columns.Count;i++)
{
if(i>0)
{
str+="\t";
}
str+=dv.Table.Columns[i].ColumnName;
}
sw.WriteLine(str);
//写内容
for(int rowNo=0;rowNo<dv.Count;rowNo++)
{
String tempstr="";
for(int columnNo=0;columnNo<dv.Table.Columns.Count;columnNo++)
{
if(columnNo>0)
{
tempstr+="\t";
}
//tempstr+=dg.Rows[rowNo,columnNo].ToString();
tempstr+=dv.Table.Rows[rowNo][columnNo].ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
}
//输出到Excel
public void ExportExcel(DataView dv)
{
SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls" ;
saveFileDialog1.FilterIndex = 0 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CreatePrompt=true;
saveFileDialog1.Title="导出Excel文件到";
DateTime now=DateTime.Now;
saveFileDialog1.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');
saveFileDialog1.ShowDialog();
Stream myStream;
myStream=saveFileDialog1.OpenFile();
StreamWriter sw=new StreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312"));
String str="";
//写标题
for(int i=0;i<dv.Table.Columns.Count;i++)
{
if(i>0)
{
str+="\t";
}
str+=dv.Table.Columns[i].ColumnName;
}
sw.WriteLine(str);
//写内容
for(int rowNo=0;rowNo<dv.Count;rowNo++)
{
String tempstr="";
for(int columnNo=0;columnNo<dv.Table.Columns.Count;columnNo++)
{
if(columnNo>0)
{
tempstr+="\t";
}
//tempstr+=dg.Rows[rowNo,columnNo].ToString();
tempstr+=dv.Table.Rows[rowNo][columnNo].ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
}
#21
http://linfuguo.cnblogs.com/archive/2006/03/23/357155.html
我的实现,VS.net2003下已经测试通过
我的实现,VS.net2003下已经测试通过
#22
多谢大家的回答,这几天一直没有时间弄,先结了贴吧!
#23
mark
#24
数据超过65536怎么办?