如何使用ExcelLibrary DLL在Excel中冻结页眉行
我正在使用ExcelLibrary DLL。
我想冻结标题行..任何机构可以build议我怎么可以做到这一点?
我已经尝试了下面的代码
Imports System.Data.SqlClient Imports ExcelLibrary Imports ExcelLibrary.SpreadSheet Partial Class DownloadExcel Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Try Dim wb As New Workbook() Dim sheetrowcounter As Integer sheetrowcounter = 0 Dim sheet As New ExcelLibrary.SpreadSheet.Worksheet("ExcelSheet") sheet.Cells(sheetrowcounter, 0) = New Cell("DELIVERY MONITORING SUMMARY(PROJECT GRANDE) ") sheet.Cells(sheetrowcounter, 2) = New Cell("") sheet.Cells(sheetrowcounter, 3) = New Cell("Report Date: '" & DateTime.Now.ToString("yyyy/MM/dd H:mm:ss tt") & " ") sheetrowcounter += 1 sheetrowcounter += 1 sheet.Cells(sheetrowcounter, 0) = New Cell("Sl No") sheet.Cells.ColumnWidth(0) = 10000 sheet.Cells(sheetrowcounter, 1) = New Cell("Name") sheet.Cells.ColumnWidth(1) = 6000 sheetrowcounter += 1 Dim t As DataTable = Session("exceltable") For i As Integer = 1 To t.Rows.Count - 1 Try sheet.Cells(sheetrowcounter, 0) = New Cell(t.DefaultView.Item(i)(0)) sheet.Cells(sheetrowcounter, 1) = New Cell(t.DefaultView.Item(i)(1)) sheetrowcounter += 1 Catch ex As Exception End Try Next sheet.Cells(0, 0).Format.FormatString = "freeze" wb.Worksheets.Add(sheet) Response.Clear() Response.ContentType = "application/vnd.ms-excel" Response.AddHeader("content-disposition", "attachment;filename=DeliveryMonitoringSummaryDaily.xls") Dim m As System.IO.MemoryStream = New System.IO.MemoryStream() wb.SaveToStream(m) m.WriteTo(Response.OutputStream) Catch ex As Exception End Try End Sub End Class
我在会话中存储具有2列和一些行的数据表(“exceltable”)对象。
我没有使用您指定的Excel Library的经验,但是我可以build议迁移到http://epplus.codeplex.com/网站上免费提供的EPPlus库。 这是非常容易使用库清晰的API,旨在创build高级Excel 2007/2010电子表格在服务器上。 我们在几个生产项目上成功地使用了它。 使用它你可以通过使用ExcelSheet.View.FreezePanes方法来实现你所需要的。 我明白,我的答案不回答你的问题,但它可以被视为替代解决scheme给你的问题(我真的不知道如果你能或不能达到你所需要的Excel库你指定)。
尝试这个:
Dim e As Worksheet ... e.Application.ActiveWindow.SplitRow = 1 e.Application.ActiveWindow.FreezePanes = True