VB.NET中基于DataTable表达式重命名BaiqiSoft LabelControl导出图片
解决方案:VB.NET中BaiqiSoft LabelControl导出图片后按DataTable字段重命名
问题描述
在VB.NET中使用BaiqiSoft LabelControl控件导出图片时,控件默认生成序列文件名(如test1.png),需要将这些文件重命名为DataTable中ProductName+Barcode格式的文件名。
现有代码
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
实现方案
由于控件本身设计为生成序列文件名,只能在导出完成后进行批量重命名。核心逻辑是:导出的文件顺序与selectedRows中的行顺序完全对应,因此可以遍历selectedRows,依次将testN.png重命名为对应行的Filename字段值。
修改后的完整Btnexport_Click方法
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() ' 新增:导出完成后执行重命名操作 Dim exportPath As String = theLabel.ExportOptions.Path For i As Integer = 0 To selectedRows.Rows.Count - 1 Dim oldFileName As String = Path.Combine(exportPath, $"test{i+1}.png") Dim newFileName As String = Path.Combine(exportPath, selectedRows.Rows(i)("Filename").ToString()) ' 检查文件是否存在,避免异常 If File.Exists(oldFileName) Then ' 处理重名情况,避免覆盖 If File.Exists(newFileName) Then newFileName = Path.Combine(exportPath, $"{Path.GetFileNameWithoutExtension(newFileName)}_{i+1}{Path.GetExtension(newFileName)}") End If File.Move(oldFileName, newFileName) End If Next End Sub
关键说明
- 导出文件的顺序与
selectedRows中行的顺序完全一致,通过索引i+1对应test{i+1}.png可保证映射准确。 - 添加了文件存在检查,避免因文件不存在或重名导致的异常;若遇到重名文件,会自动在文件名后添加序号后缀。
- 代码中已手动为
selectedRows的Filename字段赋值,无需依赖DataTable表达式,确保文件名格式正确。
内容的提问来源于stack exchange,提问作者dlaksmi
相关产品推荐
相关产品推荐

