How to Make a CSV with HTML content EXPORTABLE to MYSQL - CSV_LOADDATA [self solved]

veer-india

New Member
Joined
Jul 30, 2013
Messages
10
I could not solve the above task, and i wanted to post a query on excel forum. But then i thought lets try once more changing delimiter AND I CAME BACK WITH SOLUTION for MY OWN Task. Anyway here it is (what i typed here before).
-------------------------------------------------------------------------------------------------------------
How to save a CSV with big html content in its cells without enclosing with quotes.




I have very big excel data (have html content in its cells.) to be imported in mysql tables, For this i need to save the excel data in csv format. I can easily save it in utf8, but it doesnot have columns enclosed with quotes, when it try using vbs script to create a csv which have columns enclosed with qoutes i get error because my html also have qoutes. i tried the following vba (written by someone.)
Code:
Sub CSVFile()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
ListSep = Application.International(xlListSeparator)
  If Selection.Cells.Count > 1 Then
    Set SrcRg = Selection
  Else
    Set SrcRg = ActiveSheet.UsedRange
  End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
  CurrTextStr = ìî
For Each CurrCell In CurrRow.Cells
  CurrTextStr = CurrTextStr & """" & CurrCell.Value & """" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
  CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End Sub


the file generated have all data gone here and there, because quotes also present in the html.
then i used th || system by removing """" with || in above code (i am not vba expert)


Code:
 CurrTextStr = CurrTextStr & "||" & CurrCell.Value & "||" & ListSep


again the file generated was disorriented.




here i attach two files.
------------------------------------------------------------------------------------------------------
Solution
--------------------------------------------------------------------------------------------------
In control panel regional settings set delimiters some very different character which doesnot appear in your html.
then run this vba.
Code:
Sub CSVFile()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
ListSep = Application.International(xlListSeparator)
  If Selection.Cells.Count > 1 Then
    Set SrcRg = Selection
  Else
    Set SrcRg = ActiveSheet.UsedRange
  End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
  CurrTextStr = ìî
For Each CurrCell In CurrRow.Cells
  CurrTextStr = CurrTextStr & "|" & CurrCell.Value & "|" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
  CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End Sub

It will use pipes instead of quotes. while the delimiter set from control panel will be some very different charachter.
i use ` it.
then use the same delimiters and line terminators, field terminators in the ssh command for importing csv in mysql.
for phpmyadmin use the same method. (and happy big importing).
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top