急!! datagrid导出到excel

liguiman 2003-07-30 08:10:34
??
有两种导出方法
Dim xlsheet As New SpreadsheetClass()
Dim i As Integer = 0
Dim j As Integer = 0
'Response.End()
' 输出标题
Dim oItem As DataGridColumn
For Each oItem In MyDataGrid.Columns
xlsheet.ActiveSheet.Cells(1, i + 1) = oItem.HeaderText
'xlsheet.ActiveSheet.Range(xlsheet.ActiveSheet.Cells(1, 1),_
'xlsheet.ActiveSheet.Cells(1, i + 1)).Font.Bold = True
'设置格式
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = True
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "red"
i = i + 1
Next

Dim numbercols As Integer = MyDataGrid.Items.Item(0).Cells.Count
' 输出字段内容
For j = 0 To MyDataGrid.Items.Count - 1
For i = 0 To numbercols - 1
xlsheet.Range(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue"
'xlsheet.Range("A2:B14").WrapText = True
xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns()
xlsheet.ActiveSheet.Cells(j + 2, i + 1) = MyDataGrid.Items.Item(j).Cells(i).Text.Replace(" ", " ")
xlsheet.Columns.AutoFitColumns()
xlsheet.Rows.AutoFitColumns()

Next
Next


Response.ContentType = "application/vnd.ms-excel"
' 从Content-Type header中去除charset设置
Response.Charset = ""

' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' 获取control的HTML
DataGrid1.RenderControl(hw)
' 把HTML写回浏览器
Response.Write(tw.ToString())
Response.End()


他们的区别是什么呢?
我的Datagrid中使用自定义列,使用第二种方法时 出错
怎么改阿??


...全文
33 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
hthy 2003-08-20
  • 打赏
  • 举报
回复
mark
liguiman 2003-08-07
  • 打赏
  • 举报
回复
对不起大家
我已经找到了解决办法

xinglinshu() ( ) 信誉:100
你说的SpreadsheetClass
是要添加引用的
在项目-〉添加引用中
有关microsoft office的 就可以了
不行给我发短信息^_^
xinglinshu 2003-08-06
  • 打赏
  • 举报
回复
我是个新手,我想问一下liguiman (li) SpreadsheetClass在我这怎么是为定义的类型啊,我已经把Excel引用了啊?希望答复!!!谢谢!
dukaixy 2003-08-03
  • 打赏
  • 举报
回复
to:yuanylong(小新) 呵呵,这个程序是我写的哟

风影2000 2003-08-02
  • 打赏
  • 举报
回复
up
风影2000 2003-08-01
  • 打赏
  • 举报
回复
能不能在客户端创建一个.xls文件呢。
liguiman 2003-08-01
  • 打赏
  • 举报
回复
可以的 我都是过了
风影2000 2003-08-01
  • 打赏
  • 举报
回复
第二种方法不能实现啊。
algo 2003-07-31
  • 打赏
  • 举报
回复
楼上的,怎么做?
ylaoei 2003-07-31
  • 打赏
  • 举报
回复
gz
yuanylong 2003-07-30
  • 打赏
  • 举报
回复
我收集到一个水晶报表的程序的导出。希望能帮你:
下面是一个示例
Sub cjb()
On Error Resume Next '按班打印学生成绩表
Dim xlApp As New Excel.Application()
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")

Dim cou2 = Session("dtcx").Columns.Count
Dim m As Integer = cou2 / 2
xlApp.Cells(1, 1) = Session("t1")
xlApp.Cells(1, 1).font.name = "宋体"
xlApp.Cells(1, 1).Font.Bold = True
xlApp.Cells(1, 1).Font.Size = 12
xlApp.Cells(1, 1).Font.ColorIndex = 3

xlApp.Cells(2, 1) = Session("t2")
xlApp.Cells(2, 1).font.name = "宋体"
xlApp.Cells(2, 1).Font.Bold = True
xlApp.Cells(2, 1).Font.Size = 10
xlApp.Cells(2, 1).Font.ColorIndex = 3

xlApp.Cells(3, 1) = "重庆工商大学职业技术学院打印 时间:" + Today.Now
xlApp.Cells(3, 1).font.name = "宋体"
xlApp.Cells(3, 1).Font.Bold = True
xlApp.Cells(3, 1).Font.Size = 10
xlApp.Cells(3, 1).Font.ColorIndex = 3

Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Session("dtcx").Columns
colIndex = colIndex + 1
xlApp.Cells(4, colIndex) = Col.ColumnName

xlApp.Cells(4, colIndex).Font.Name = "宋体"
xlApp.Cells(4, colIndex).Font.Bold = True
xlApp.Cells(4, colIndex).Font.size = 9
xlApp.Cells(4, colIndex).Borders.LineStyle = 1

xlApp.Cells(4, colIndex).columnwidth = 5
' xlApp.Cells(3, colIndex).RowHeight = 30
xlApp.Cells(4, colIndex).HorizontalAlignment = 2
xlApp.Cells(4, colIndex).VerticalAlignment = 2
xlApp.Cells(4, colIndex).WrapText = True
xlApp.Range("a1").ColumnWidth = 11
xlApp.Range("b1").ColumnWidth = 5
Next
'得到的表所有行,赋值给单元格
rowIndex = 3
For Each Row In Session("dtcx").Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Session("dtcx").Columns
colIndex = colIndex + 1
If Col.ColumnName = "学号" Then
xlApp.Cells(rowIndex + 1, colIndex) = "'" + Row(Col.ColumnName)
Else
xlApp.Cells(rowIndex + 1, colIndex) = Row(Col.ColumnName)
End If
' xlApp.Cells(rowIndex + 1, colIndex) = Row(Col.ColumnName)
xlApp.Cells(rowIndex + 1, colIndex).Font.Name = "宋体"
xlApp.Cells(rowIndex + 1, colIndex).Font.size = 10
xlApp.Cells(rowIndex + 1, colIndex).Borders.LineStyle = 1
Next
Next
xlApp.Visible = True
xlBook.Saved = True
xlApp.UserControl = False
Dim mm = Server.MapPath(".") + "\temp\" + Session("username") + "cjb.xls" '服务器保存地址
xlApp.ActiveWorkbook.SaveCopyAs(mm)
' xlApp.Sheets.PrintOut()
xlApp.Quit()

End Sub
lwgj 2003-07-30
  • 打赏
  • 举报
回复
看看
http://xml.sz.luohuedu.net/xml/ShowList.asp?id=1
cyp503 2003-07-30
  • 打赏
  • 举报
回复
用了模板列?
加上:
DataGrid.Columns[DataGrid.Columns.Count-1].Visible=false;
chenee543216 2003-07-30
  • 打赏
  • 举报
回复
-----》》可以看一下,要是很多人同时在服务器上生成excel那还能行吗?
还有,就是文件名称,不能其同一个文件名吧?
怎样保证每个人在看过,或下载之后,不用手动,而是自动将文件删除呢?



you could let all the users to export the dataGrid to client side(i.e. download )
not at the serverside



liguiman 2003-07-30
  • 打赏
  • 举报
回复
cyp503(谁怕?一蓑烟雨任平生) (

在哪里呢加呢?
liguiman 2003-07-30
  • 打赏
  • 举报
回复
:)
我想问的是解决办法!
这样的程序我都看过了
我所提的两种方法有什么不同?
可以看一下,要是很多人同时在服务器上生成excel那还能行吗?
还有,就是文件名称,不能其同一个文件名吧?
怎样保证每个人在看过,或下载之后,不用手动,而是自动将文件删除呢?

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧