Home » excel » vb.net – Convert xls File to csv, but extra rows added?

vb.net – Convert xls File to csv, but extra rows added?

Posted by: admin May 14, 2020 Leave a comment


So, I am trying to convert some xls files to a csv, and everything works great, except for one part. The SaveAs function in the Excel interop seems to export all of the rows (including blank ones). I can see these rows when I look at the file using Notepad. (All of the rows I expect, 15 rows with two single quotes, then the rest are just blank). I then have a stored procedure that takes this csv and imports to the desired table (this works on spreadsheets that have been manually converted to csv (e.g. open, File–> Saves As, etc.)

Here is the line of code I am using for my SavesAs in my code. I have tried xlCSV, xlCSVWindows, and xlCSVDOS as my file format, but they all do the same thing.

wb.SaveAs(aFiles(i).Replace(".xls", "B.csv"), Excel.XlFileFormat.xlCSVMSDOS, , , , False) 'saves a copy of the spreadsheet as a csv

So, is there some additional step/setting I need to do to not get the extraneuos rows to show up in the csv?

Note that if I open this newly created csv, and then click Save As, and choose csv, my procedure likes it again.

How to&Answers:

When you create a CSV from a Workbook, the CSV is generated based upon your UsedRange. Since the UsedRange can be expanded simply by having formatting applied to a cell (without any contents) this is why you are getting blank rows. (You can also get blank columns due to this issue.)

When you open the generated CSV all of those no-content cells no longer contribute to the UsedRange due to having no content or formatting (since only values are saved in CSVs).

You can correct this issue by updating your used range before the save. Here’s a brief sub I wrote in VBA that would do the trick. This code would make you lose all formatting, but I figured that wasn’t important since you’re saving to a CSV anyway. I’ll leave the conversion to VB.Net up to you.

Sub CorrectUsedRange()
    Dim values
    Dim usedRangeAddress As String
    Dim r As Range
    'Get UsedRange Address prior to deleting Range
    usedRangeAddress = ActiveSheet.UsedRange.Address
    'Store values of cells to array.
    values = ActiveSheet.UsedRange
    'Delete all cells in the sheet
    'Restore values to their initial locations
    Range(usedRangeAddress) = values
End Sub


Tested your code with VBA and Excel2007 – works nice.

However, I could replicate it somewhat, by formatting an empty cell below my data-cells to bold. Then I would get empty single quotes in the csv. BUT this was also the case, when I used SaveAs.

So, my suggestion would be to clear all non-data cells, then to save your file. This way you can at least exclude this point of error.


I’m afraid that may not be enough. It seems there’s an Excel bug that makes even deleting the non-data cells insufficient to prevent them from being written out as empty cells when saving as csv.



Another way, without a script. Hit Ctrl+End . If that ends up in a row AFTER your real data, then select the rows from the first one until at least the row this ends up on, right click, and “Clear Contents”.