如何在VB.NET中根据表达式数据表从文件名顺序导出后重命名文件

问题描述 投票:0回答:1

我正在尝试在 VB.NET 中使用数据表导出后重命名文件图像

我使用来自 BaiqiSoft 的库控件 LabelControl for .NET

我也向百奇软件寻求过支持,他回答说“这是我们产品的设计行为”。因此,从库中导出文件名的结果是连续的。是否有一种解决方案可以在导出图像后重命名,并且根据表达式数据表重命名文件名而不会出现错误?或者其他方法?

请指导我

有关更详细的文档,请参阅下面的链接。

https://www.mysofttool.com/help/labeldesign/

Imports System.IO
Imports BaiqiSoft.LabelControl
Public Class Form1
    Private m_DataTable As DataTable
    Private Sub CreateDataTable()
        If m_DataTable IsNot Nothing Then Return
        m_DataTable = New DataTable
        'Columns
        m_DataTable.Columns.Add("ProductName", GetType(String))
        m_DataTable.Columns.Add("Barcode", GetType(String))
        m_DataTable.Columns.Add("Price", GetType(Single))
        m_DataTable.Columns.Add("LabelNumber", GetType(Integer))
        m_DataTable.Columns.Add("QTY", GetType(Integer))
        m_DataTable.Columns.Add("Filename", GetType(String), "ProductName +','+ Barcode")
        'Rows
        m_DataTable.Rows.Add("Mishi Kobe Niku", "845723054943", 96.0, 2, 1)
        m_DataTable.Rows.Add("Carnarvon Tigers", "246321456231", 61.5, 1, 1)
        m_DataTable.Rows.Add("Ipoh Coffee", "589412354786", 46.0, 3, 1)
        m_DataTable.Rows.Add("Aniseed Syrup", "457125463254", 10.0, 1, 1)
        m_DataTable.Rows.Add("Teatime Chocolate Biscuits", "232145674321", 9.2, 5, 1)
    End Sub
Private Sub Btnexport_Click(sender As Object, e As EventArgs) Handles Btnexport.Click
        Dim theLabel As New LabelPrinting()
        theLabel.LicenseKey = ""
        theLabel.OpenLabel(Application.StartupPath & "\test.blf")
        Dim selectedRows As DataTable = m_DataTable.Clone
        selectedRows.Columns("Filename").Expression = Nothing : selectedRows.Columns("Filename").ReadOnly = False
        For Each row2 As DataGridViewRow In DataGridView1.Rows
            Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
            If isselect Then
                Dim newRow As DataRow = selectedRows.NewRow()
                newRow("ProductName") = m_DataTable.Rows(row2.Index)("ProductName")
                newRow("Barcode") = m_DataTable.Rows(row2.Index)("Barcode")
                newRow("Price") = m_DataTable.Rows(row2.Index)("Price")
                newRow("LabelNumber") = m_DataTable.Rows(row2.Index)("LabelNumber")
                newRow("QTY") = m_DataTable.Rows(row2.Index)("QTY")
                newRow("Filename") = m_DataTable.Rows(row2.Index)("ProductName") + "," + m_DataTable.Rows(row2.Index)("Barcode") + ".png"
                selectedRows.Rows.Add(newRow)
                theLabel.Label.QuantityColumn = "QTY"
            End If
        Next row2
        theLabel.DataSource = selectedRows
        theLabel.ExportOptions.FileName = "test"
        theLabel.ExportOptions.Path = Application.StartupPath
        theLabel.ExportOptions.Quantity = QuantityOptions.AllRecords
        theLabel.ExportOptions.Format = ImageFormats.Png
        theLabel.ExportOptions.Resolution = 300
        theLabel.ExportImage()
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateDataTable()
        DataGridView1.DataSource = m_DataTable
        Dim CheckedBoxColumn As New DataGridViewCheckBoxColumn
        CheckedBoxColumn.Width = 40
        CheckedBoxColumn.Name = "checkboxcolumn"
        CheckedBoxColumn.HeaderText = "Check"
        DataGridView1.Columns.Insert(0, CheckedBoxColumn)
    End Sub
End Class

代码结果:

test1.png

test2.png

test3.png

test4.png

test5.png

应该是这样的(期望的结果):

Mishi Kobe Niku,845723054943.png
 
Carnarvon Tigers,246321456231.png
 
Ipoh Coffee,589412354786.png
 
Aniseed Syrup,457125463254.png
 
Teatime Chocolate Biscuits,232145674321.png

我可以这样重命名吗

test1.png >>>> Mishi Kobe Niku,845723054943.png

test2.png >>>> Carnarvon Tigers,246321456231.png

test3.png >>>> Ipoh Coffee,589412354786.png

test4.png >>>> Aniseed Syrup,457125463254.png

test5.png >>>> Teatime Chocolate Biscuits,232145674321.png
vb.net loops filenames file-rename system.io.directory
1个回答
0
投票

您应该能够稍后重命名文件,只需将原始名称映射到所需的新名称,例如在

Dictionary(Of String, String)
:

Private Sub Btnexport_Click(sender As Object, e As EventArgs) Handles Btnexport.Click
    Dim theLabel As New LabelPrinting()
    theLabel.LicenseKey = ""
    theLabel.OpenLabel(Application.StartupPath & "\test.blf")
    Dim selectedRows As DataTable = m_DataTable.Clone
    selectedRows.Columns("Filename").Expression = Nothing : selectedRows.Columns("Filename").ReadOnly = False
    
    Dim fileNameMapper = New Dictionary(Of String, String)(StringComparer.OrdinalIgnoreCase)
    Dim fileCounter As Int32 = 0
    Dim fileName = "test"

    For Each row2 As DataGridViewRow In DataGridView1.Rows
        Dim isselect As Boolean = Convert.ToBoolean(row2.Cells("checkboxcolumn").Value)
        If isselect Then
            Dim newRow As DataRow = selectedRows.NewRow()
            newRow("ProductName") = m_DataTable.Rows(row2.Index)("ProductName")
            newRow("Barcode") = m_DataTable.Rows(row2.Index)("Barcode")
            newRow("Price") = m_DataTable.Rows(row2.Index)("Price")
            newRow("LabelNumber") = m_DataTable.Rows(row2.Index)("LabelNumber")
            newRow("QTY") = m_DataTable.Rows(row2.Index)("QTY")
            newRow("Filename") = m_DataTable.Rows(row2.Index)("ProductName") + "," + m_DataTable.Rows(row2.Index)("Barcode") + ".png"
            selectedRows.Rows.Add(newRow)
            theLabel.Label.QuantityColumn = "QTY"

            Dim originalName = $"{fileName}{++fileCounter}.png"
            fileNameMapper.Add(originalName, newRow.Field(Of String)("Filename"))
        End If
    Next row2
    theLabel.DataSource = selectedRows
    theLabel.ExportOptions.FileName = fileName
    theLabel.ExportOptions.Path = Application.StartupPath
    theLabel.ExportOptions.Quantity = QuantityOptions.AllRecords
    theLabel.ExportOptions.Format = ImageFormats.Png
    theLabel.ExportOptions.Resolution = 300
    theLabel.ExportImage()
    RenameFiles(fileNameMapper, theLabel.ExportOptions.Path)
End Sub

Private Sub RenameFiles(fileNameMapper As Dictionary(Of String,String), path As String)
    Dim dir As New DirectoryInfo(path)
    Dim files = dir.EnumerateFiles(path, "*.png").
        Where(Function(fi) fileNameMapper.ContainsKey(fi.Name))
    For Each file In files
        Dim destination = IO.Path.Combine(file.Directory.FullName, fileNameMapper(file.Name))
        file.MoveTo(destination, true)
    Next
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.