Excel报告生成

时间:2009-09-03 06:43:25

标签: vb.net excel

如何设置列宽并将前10行留空以便将图像插入Excel报告?

这是我的代码:

If ComDset.Tables(0).Rows.Count > 0 Then
    Try
        With Excel
            .SheetsInNewWorkbook = 1
            .Workbooks.Add()
            .Worksheets(1).Select()

            Dim i As Integer = 1
            For col = 0 To ComDset.Tables(0).Columns.Count - 1
                .cells(1, i).value = ComDset.Tables(0).Columns(col).ColumnName
                .cells(1, i).EntireRow.Font.Bold = True
                i += 1
            Next

            i = 2

            Dim k As Integer = 1
            For col = 0 To ComDset.Tables(0).Columns.Count - 1
                i = 2
                For row = 0 To ComDset.Tables(0).Rows.Count - 1
                    .Cells(i, k).Value = ComDset.Tables(0).Rows(row).ItemArray(col)
                    i += 1
                Next
                k += 1
            Next

            filename = "ShiftReport" & Format(MdbDate, "dd-MM-yyyy") & ".xls"
            .ActiveCell.Worksheet.SaveAs(filename)
        End With

        System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
        Excel = Nothing
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    ' The excel is created and opened for insert value. We most close this excel using this system
    Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
    For Each i As Process In pro
        i.Kill()
    Next
End If

1 个答案:

答案 0 :(得分:0)

Range.ColumnWidth属性 返回或设置指定范围内所有列的宽度。读/写Variant。