五千年(敝帚自珍)

主题:【求助】VB.Net高手们进来瞅一瞅啦, CR问题 -- MacArthur

共:💬24 🌺14
全看分页树展 · 主题 跟帖
家园 CrystalReport去死吧

终于发现了一个极其简单的办法 -- 本来也是,Excel也是微软的,微软怎么会不照顾自己一家子呢?

在Project里面add reference -> COM object -> MS Excel 11.0 -- 加了这个之后就可以在VB里名正言顺地引用微软的一堆便民服务... 瞅人这名儿起的,Microsoft.Office.Core -- 一core在手,谁敢阻拦?

这个CrystalReport呢,功能的确比较强大,而且formatting出来也很漂亮。不过呢,出了个连他们自己都解释不了的问题,不能用还不全白搭么... 而且俺只关心数据,不在乎格式 -- 只求您能把数据全部灌到报表里就是大功一件啦...

附上例程,供有心者参考...

Imports Microsoft.Office.Core

......

......

Sub generateCryatalReport(ByVal strReportName As String)

Dim datatable As System.Data.DataTable = dsWKIT.Tables(0)

Dim Application As New Excel.Application()

Application.SheetsInNewWorkbook = 1

Dim wb As Excel.Workbook = Application.Workbooks.Add()

Dim ws As Excel.Worksheet = wb.Worksheets("Sheet1")

ws.Name = strReportName

Dim rowIndex As Integer

Dim colIndex As Integer

Try

' Create the headers on the sheet.

For colIndex = 1 To datatable.Columns.Count

ws.Cells(1, colIndex) = datatable.Columns(colIndex - 1).ColumnName

Next

' Add each row of data to the sheet.

' The sheet cell row is incremented by one because the first row was used for the header.

For rowIndex = 1 To datatable.Rows.Count

For colIndex = 1 To datatable.Columns.Count

ws.Cells(rowIndex + 1, colIndex) = datatable.Rows(rowIndex - 1)(colIndex - 1).ToString()

Next

Next

Catch ex As System.Runtime.InteropServices.COMException

End Try

wb.SaveAs("C:\" & strReportName & ".xls")

wb.Close()

Application.Quit()

End Sub

全看分页树展 · 主题 跟帖


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河