1

如何.XLSX在 Web 应用程序中使用 EPPlus 删除文件的列?

我使用 EPplus 生成 Excel 报告和一个存储过程来从数据库中获取数据。

问题是我想通过 EPplus 删除报告文件中的一列信息(不应更改存储过程。)

我会删除其中的附加列,并且还想将页面布局方向更改为(从右到左),但它不起作用

'----/// Click Report Button ///----
Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        If "MYcondition is true" Then
            GenerateXLSXFile(CreateDataTable())
        End If
End Sub

'----/// Generate Report ///----
Private Sub GenerateXLSXFile(ByVal tbl As DataTable)
        Dim excelPackage = New ExcelPackage
        Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("My_Worksheet")

        excelWorksheet.View.ShowGridLines = False
        excelWorksheet.Cells.Style.Border.Bottom.Style = Style.ExcelBorderStyle.Thick

        excelWorksheet.Cells("A5").LoadFromDataTable(tbl, True)

       '-----/// Hide a Column ///---------
        excelWorksheet.Column(2).Hidden = True    


       '----/// Change PageLayout Direction ///---------------  
        excelWorksheet.View.PageLayoutView = excelWorksheet.View.RightToLeft  

        excelWorksheet.Cells("A5").Value = "header_1"
        excelWorksheet.Cells("B5").Value = "header_2"
        excelWorksheet.Cells("C5").Value = "header_3"

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("content-disposition", "attachment;  filename=My_ExcelName.xlsx")

        Dim stream As MemoryStream = New MemoryStream(excelPackage.GetAsByteArray())
        Response.OutputStream.Write(stream.ToArray(), 0, stream.ToArray().Length)
        Response.Flush()
        Response.Close()
End Sub

'----/// Create Data Table for Exel Report (use stored procedure) ///----
Private Function CreateDataTable() As DataTable
        Dim dataTable As New DataTable("tbl_Name")
        Dim dataAdapter As New SqlDataAdapter()

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ProvidersCS").ToString)
        Dim cmd As New SqlCommand("My Select Command", conn)

        cmd.CommandType = CommandType.StoredProcedure
        Try
            conn.Open()
            dataAdapter.SelectCommand = cmd
            dataAdapter.Fill(dataTable)
        Catch ex As Exception
        Finally
            conn.Close()
        End Try

        Return dataTable        
End Function
4

1 回答 1

1

EPPlus API 上的插入和删除列方法尚未实现,因为它不适用于具有当前单元存储设计的大型工作表。

删除列的唯一方法是自己移动单元格。

本主题是关于 EPPlus Codeplex Page 的讨论。

参考= http://epplus.codeplex.com/discussions/262729

于 2013-08-07T20:55:14.353 回答