参数"单元格" -method

时间:2016-01-11 15:40:11

标签: vb.net export-to-excel epplus

我是VB.NET的新手。我想从VB.NET导出Excel,我在项目中使用EPPlus。

此代码中ws.cells()的四个参数是什么?

代码:

 Imports OfficeOpenXml
 Imports OfficeOpenXml.Style
 Imports System.IO
 Public Class excelExport
 Private Access As New DBControl
 Public Sub myReport()
    Dim saveDialog As New SaveFileDialog
    saveDialog.Filter = "Excel File (*.xlsx)|*.xlsx"
    saveDialog.FilterIndex = 1

    If saveDialog.ShowDialog() = DialogResult.OK Then
        Try

            Dim file As FileInfo = New FileInfo(saveDialog.FileName)

            ' Ensures we create a new workbook
            If (file.Exists) Then
                file.Delete()
            End If

            Dim pck As ExcelPackage = New ExcelPackage(file)

            ' Add a new worksheet to the empty workbook
            Dim ws As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet1")
            '  Load data from DataTable to the worksheet
            ws.Cells("A1").Value = "new"
            ws.Cells.AutoFitColumns()

            '  Add some styling
            Dim rng As ExcelRange = ws.Cells(1, 1, 1, 10) '<----------  This code

            rng.Style.Font.Bold = True
            rng.Style.Fill.PatternType = ExcelFillStyle.Solid
            rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189))
            rng.Style.Font.Color.SetColor(System.Drawing.Color.White)

            ' Save the new workbook
            pck.Save()


            MessageBox.Show(String.Format("Excel file {0} generated successfully.", file.Name))

        Catch ex As Exception

            MessageBox.Show("Failed to export to Excel. Original error: " + ex.Message)
        End Try
    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:1)

您的第一个问题主要是征求意见,这不是本网站的用途。值得一提的是,我发现EPPlus对于创建Excel文件非常有用。

第二个问题的答案是Cells方法中的四个参数是:

  1. 范围中包含的第一行的编号。
  2. 要包含在范围中的第一列的编号。
  3. 范围中包含的最后一行的编号。
  4. 要包含在范围中的最后一列的编号。