【问题标题】:phpmyadmin/CSV(Excel) Problemsphpmyadmin/CSV(Excel) 问题
【发布时间】:2011-07-14 22:19:47
【问题描述】:

我有一个 Excel 工作簿,其中 .csv 文件使用此 vba 代码输出并带有引号:

 'Set up Delimiters
DELIMITER = ","
QUOTE = """"

 'Set up file to save, ask user for name
GetFileName = InputBox("Enter Name for semi-colon delimited file: (Do not enter file extension)", "FILENAME")
  'CurrentPath = Application.ActiveWorkbook.Path
 PathtoUse = "C:\Users\David\Desktop\"
 FileNametoUse = PathtoUse & GetFileName & ".csv"

 'Find cells to cycle through
With ActiveSheet.UsedRange
    LastRow = .Cells(.Cells.Count).Row
    LastCol = .Cells(.Cells.Count).Column
End With

 'Assign a handle to next file#
FileNum = FreeFile

 'Open and write to file named above
Open FileNametoUse For Output As #FileNum

 'Cycle through rows and cols
For Each CurrentRow In Range("A1:A" & LastRow)
    With CurrentRow 'Now cycle through each cell in the row
        For Each CurrentCell In Range(.Cells, Cells(.Row, LastCol))
             'If the cell contains Non-Numeric (IsNumeric=False) then put quotes around the info
            If IsNumeric(CurrentCell.Text) = False Then
                CellData = QUOTE & CurrentCell.Text & QUOTE
            Else 'The cell contains numeric, use contents as is
                CellData = CurrentCell.Text
            End If
             'as the code cycles, keep adding each col info to string
            LineOutput = LineOutput & DELIMITER & CellData
        Next CurrentCell

         'Remove the first 2 chars in the line (since the delimiter is put in first)
        LineOutput = Mid(LineOutput, 2)
         'Print the line to the file
        Print #FileNum, LineOutput
          'Clear out the variable
        LineOutput = Empty
    End With
Next CurrentRow

 'Close the file
Close #FileNum

这会正确输出文件,例如:

"FirstName","Surname","YBC","BTBA","JTE","EnteringAverage","DOB","Game1","Game2","Game3","Game4","Game5","Game6","Game7","Game8","IndividualTotal","Average","TeamTotal"
"David","Passmore","Bowlplex Poole",116016,193,179,"05/08/1994",203,254,211,195,187,184,200,267,1701,212.63,3178
"Callum","Bailey","Bowlplex Poole",016015,185,189,"30/05/1996",175,145,195,117,201,265,221,158,1477,184.63,3178
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","",""

首先,我想知道如何删除所有空白单元格,其次,当我将其输入 phpMyAdmin 时,它一切正常,但它错过了第一列,它输入了我不想要的列名,所以我想要它输入所有名字列,然后我希望它错过列名。

我的服务器的 phpmyadmin 版本是 2.6.4(我知道它很旧,但我对此无能为力)

但在我的本地服务器上也是如此,它是版本 3.3.9

更新

我已经解决了第一列的问题,只需将文件向右移动一列,这样就解决了,但其他两个问题仍然存在。

【问题讨论】:

    标签: mysql excel import


    【解决方案1】:

    您可以在这里更改您的 vba:

     'Set up Delimiters 
    DELIMITER = "," 
    QUOTE = """"  
    

    到这里:

    'Set up Delimiters
    DELIMITER = ";"
    QUOTE = """" 
    

    因为 phpMyAdmin 默认为 ;作为分隔符。

    在回答您的第二点时,您可以使用此 vba 代码删除空白单元格:

    Sub MacroNAME()
        ActiveWorkbook.Save
        Rows("1:1").Select
        Selection.Cut
        Sheets("sheetname").Select
        Sheets.Add
        ActiveSheet.Name = "Save"
        ActiveSheet.Paste
        Sheets("sheetname").Select
        Rows("1:1").Select
        Rows("1:1").RowHeight = 14.25
        Selection.Delete Shift:=xlUp
        Cells.Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.ClearContents
        Columns("A:A").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp
        Rows("2:2").RowHeight = 14.25
        **Code Used above here**
        Rows("1:1").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
        Rows("1:1").RowHeight = 0.1
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft
        Sheets("sheetname").Select
        Rows("2:2").Select
        Selection.Insert Shift:=xlDown
        Rows("1:1").RowHeight = 10.5
        Rows("1:1").RowHeight = 14.25
        Sheets("Save").Select
        Selection.Cut
        Sheets("sheetname").Select
        Rows("1:1").Select
        ActiveSheet.Paste
        Application.DisplayAlerts = False
        Sheets("Save").Select
        ActiveWindow.SelectedSheets.Delete
        Application.DisplayAlerts = True
    

    这将清除列标题,前提是它们是第一行。

    你可以把它应用到一个按钮上,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 2014-04-19
      • 1970-01-01
      • 2011-05-07
      • 2020-11-08
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多