我正在开发一个应用程序,该应用程序读取一个大的制表符分隔的文本文件,并返回一个包含多个具有相同数据表的excel文件。
我已经能够成功创建文件。但是,我遇到了一个问题。当我将文件导入到新创建的excel文件中时,导入第一个文件时该文件将打开。我不希望发生这种情况,我希望在所有文件成功导入后由用户打开它。
有趣的是,当我最初调试应用程序时,这没有发生。所以我对为什么现在发生感到困惑。可能是因为我缺少查询表的成员吗?
能帮我吗?谢谢!
编辑:我发现向文件添加标题是导致此问题的原因。但是,在将它们编辑到无法理解的程度之后,我仍然无法使其正常工作。
标题:
encabezados = {"Name",
"Description",
"listingId", "SKU", "price", "quantity", "Date", "image", "item",
"productId", "Shipping", "note", "condition", "category1", "browsepath", "storefrontfeature",
"asin", "asins", "asinss", "willshipinternationally", "expeditedshipping", "zshopboldface", "producid", "bidForfeaturedplacement", "adddelete",
"pendingquantity", "fulfillmentchannel", "BusinessPrice", "QuantityPriceType", "QuantityLowerBound", "QuantityPrice", "QuantityLowerBounds",
"QuantityPrices", "QuantityLowerBoundss", "QuantityPricess", "QuantityLowerBoundsss", "QuantityPricesss", "QuantityLowerBoundssss",
"QuantityPricessss", "merchantshippinggroup"}
功能:
Private Sub createExcel(pagina)
Dim hoja = 1
Dim excel As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
If excel Is Nothing Then
MessageBox.Show("Error. Excel 2016 no está instalado en esta computadora", "Error")
Exit Sub
End If
Dim iSheetsPerBook As Integer
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
excel.SheetsInNewWorkbook = pagina
For i = 1 To pagina
iSheetsPerBook = excel.SheetsInNewWorkbook
xlWorkBook = excel.Workbooks.Add(misValue)
excel.SheetsInNewWorkbook = iSheetsPerBook
Next
For x = 1 To pagina
Dim archivo = String.Format("{0}\ArchivosExcel\excel{1}.txt", dondeEstaElArchivo, x)
xlWorkSheet = xlWorkBook.Worksheets(hoja)
Dim qt As QueryTable = xlWorkSheet.QueryTables.Add(Connection:=String.Format("TEXT;{0}", archivo), Destination:=xlWorkSheet.Range("$A$1"))
With qt
.Name = "Import"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = XlTextParsingType.xlDelimited
.TextFileTextQualifier = XlTextQualifier.xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = GetColumnDataTypes(xlWorkSheet.Columns.Count)
.TextFileTrailingMinusNumbers = True
.Refresh(BackgroundQuery:=False)
End With
hoja += 1
Next
Dim saveDialog As New SaveFileDialog()
saveDialog.Title = "Por favor elija donde quiere guardar el archivo"
saveDialog.Filter = "Excel 2007-on (*.xlsx)|*.xlsx"
Dim respuesta = saveDialog.ShowDialog()
If respuesta = DialogResult.Cancel Or saveDialog.FileName = "" Then
Exit Sub
End If
Dim fileSave = saveDialog.FileName
xlWorkBook.SaveAs(fileSave)
xlWorkBook.Close(True, misValue, misValue)
excel.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(excel)
End Sub
Private Function GetColumnDataTypes(queryTableColumnsCount As Long) As Object
Dim textDataTypes As XlColumnDataType()
textDataTypes = Enumerable.Repeat(XlColumnDataType.xlTextFormat, queryTableColumnsCount).ToArray()
Return textDataTypes
End Function
答案 0 :(得分:0)
我发现了原因:
此问题是由于在文本文件导入之前 插入了标头引起的。我最终在导入后插入了它们,它的工作就像一个魅力。