VB 通过指定Excel模板文件进行另存为新文件操作

Private Sub cmdExport_Click()
    Dim strTemplateFile         As String
    Dim strFileName             As String
    Dim FSO                     As New FileSystemObject
    Dim excelApp                As Excel.Application
    Dim excelBook               As Excel.Workbook
    Dim excelSheet              As Excel.Worksheet
    Dim lngLineNo               As Long
    Dim i                       As Long
    
    On Error GoTo ErrHandle


    strTemplateFile = gStrXlt & "\模板文件名.xls"
    If Not FSO.FileExists(strTemplateFile) Then
        MsgBox "模板文件不存在", vbCritical, Me.Caption
        Exit Sub
    End If
    
    strFileName = gStrOther & "\新文件名" & Format(Date, "YYYYMMDD") & ".xls"
    
    If FSO.FileExists(strFileName) Then
        FSO.DeleteFile strFileName
    End If
    
    Set excelApp = CreateObject("Excel.Application")
    Set excelBook = excelApp.Workbooks.Open(strTemplateFile)
    Set excelSheet = excelBook.Worksheets(1)
    
    
    excelApp.Visible = False
    excelApp.DisplayAlerts = False         '禁止Excel提示
    excelApp.Columns("A:L").NumberFormatLocal = "@"  '设置成文本格式
    
    
    With prg
        .Max = lvData.ListItems.Count
        .Min = 0
        .Value = 0
    End With
    lngLineNo = 4        '从第四行开始写
    For i = 1 To lvData.ListItems.Count
        excelSheet.Cells(lngLineNo, 1) = lvData.ListItems(i).SubItems(1)                            
        excelSheet.Cells(lngLineNo, 2) = lvData.ListItems(i).SubItems(2)                           
        excelSheet.Cells(lngLineNo, 3) = lvData.ListItems(i).SubItems(3)                            
        excelSheet.Cells(lngLineNo, 4) = lvData.ListItems(i).SubItems(4)                            
        excelSheet.Cells(lngLineNo, 5) = lvData.ListItems(i).SubItems(5)                            
        lngLineNo = lngLineNo + 1
        If prg.Value < prg.Max Then
            prg.Value = prg.Value + 1
        End If
        DoEvents
    Next
    prg.Value = prg.Max
    
    With excelSheet
        .Range(.Cells(1, 1), Cells(lvData.ListItems.Count + 3, 5)).Borders.LineStyle = xlContinuous
        .Range(.Cells(1, 1), Cells(lvData.ListItems.Count + 3, 5)).Font.Size = 9
    End With
    
    excelBook.Saved = True
    excelBook.SaveAs strFileName
    '关闭Excel进程
    excelBook.Close
    excelApp.Quit
    
    Set excelBook = Nothing
    Set excelApp = Nothing
    
    MsgBox "导出完毕!" & vbCrLf & "文件路径:" & strFileName, vbInformation, Me.Caption


    On Error GoTo 0
    Exit Sub
ErrHandle:
    Call gErrList("frmFenQiQiShuRpt.cmdExport_Click", Err.Description, Err.Number, True)


End Sub
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 .NET Framework 自带的 System.Drawing.Imaging 命名空间中的 TiffBitmapDecoder 和 CroppedBitmap 类来读取和处理 TIFF 图片的部分区域。下面是一个示例代码,可以读取指定区域的 TIFF 图片,并将其另存为的图片: ```vb.net Dim decoder As TiffBitmapDecoder = New TiffBitmapDecoder(New Uri("original.tiff"), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default) Dim source As BitmapSource = decoder.Frames(0) Dim rect As Int32Rect = New Int32Rect(10, 10, 100, 100) ' 定义要读取的区域 Dim cropped As CroppedBitmap = New CroppedBitmap(source, rect) ' 读取指定区域的图片 Dim encoder As JpegBitmapEncoder = New JpegBitmapEncoder() ' 创建 JPEG 编码器 encoder.Frames.Add(BitmapFrame.Create(cropped)) ' 将裁剪后的图片添加到编码器中 Using stream As FileStream = File.Create("cropped.jpg") ' 创建文件流并保存图片 encoder.Save(stream) End Using ``` 在上面的代码中,我们首先创建了一个 TiffBitmapDecoder 对象来读取原始 TIFF 图片。然后,我们获取了第一帧图片(如果 TIFF 文件包含多帧图片,则需要修改该代码以读取指定帧)。接着,我们定义了一个 Int32Rect 对象来指定要读取的区域。然后,我们使用 CroppedBitmap 类的构造函数来裁剪指定区域的图片,并将其存储在一个的 CroppedBitmap 对象中。最后,我们创建了一个 JpegBitmapEncoder 对象来将裁剪后的图片编码为 JPEG 格式,并使用 Save 方法将其保存到文件中。 请注意,上述示例代码中的区域参数可能需要根据实际需要进行调整。另外,如果你需要处理多个 TIFF 图片,可以将上述代码放在一个循环中,对每张图片进行处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值