How can I delete a Column of XLSX file with EPPlus in web app

无人久伴 提交于 2020-01-03 03:21:06

问题


How can I delete a column of a .XLSX file with EPPlus in a web application?

I use EPplus for generating Excel reports and a stored procedure to get the data from database.

The problem is that I want to remove one of the columns of information in the report file by EPplus (stored procedure should not be changed.)

I would remove the additional column in and also want to change the page layout direction to (right to left), but it does not work

'----/// 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

回答1:


The Insert and Remove Columns Method on EPPlus API is not yet implemented because it would not work well with large worksheets with the current design of the cell store.

The only way to delete a column is to move the cells yourself.

This topic is a discussion on EPPlus Codeplex Page.

Reference= http://epplus.codeplex.com/discussions/262729



来源:https://stackoverflow.com/questions/14170717/how-can-i-delete-a-column-of-xlsx-file-with-epplus-in-web-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!