1

I have an application that can import data from an Excel (.xls) spreadsheet. This spreadsheet is in turn imported from a CSV file, which provides a template for data. The user then populates the template, saves it as an .xls file, and imports it into the application.

My problem is that the application that imports the spreadsheet expects a sheet called Sheet1, but when the user opens the CSV template, Excel 'imports' the CSV data into a and automatically created worksheet with the same name as the CSV file. Example:

  1. Application X creates a template file called January.csv
  2. User opens January.csv in Excel, which creates a worksheet called January. They populate this worksheet, and save the workbook as MonthlyData.xls
  3. Application Y attempts to import data from Sheet1 in MonthlyData.xls, and fails.

I would like to find some way of telling Excel that when it opens a CSV file, it must automatically rename the first worksheet it creates to Sheet1, and not give that sheet the same name as the CSV file, as it does by default. What means are at my disposal for doing this?

1
  • As far as I know, with the Excel API you could also take the FIRST sheet, instead of a named one...
    – sinni800
    Jan 18, 2012 at 12:17

1 Answer 1

1

There is no such thing as a 'worksheet' in a CSV file; therefore, there is nothing to rename. Rely on the behavior you have explained above and pass the file name from the original step to the final step so that the XLS import process in step 3 knows to expect a object called original_filename from step 1.

A CSV is just that - a text file with comma separated values, there is no way to define a worksheet in a CSV file.

2
  • 1
    Of course there isn't a worksheet in a CSV file, but when you open a CSV file in Excel, Excel creates a worksheet in order to present the data from the CSV file, and it gives this worksheet a name, so of course there is something to rename, except the renaming will only make sense if you then save the document as a workbook, which is what I describe above.
    – ProfK
    Jan 18, 2012 at 19:25
  • 1
    I don't think you follow. Excel does not create a worksheet when it displays a CSV. The first workbook reflects the name of the CSV file. If you were to rename the sheet and attempt save it, Excel would warn you that you have features in the file that it can't save and recommend using a XLS/XLSX. If you were to proceed in CSV (click yes), and close/reopen the file, you would see that "sheet" would be named the same as the filename and never took your changes. The CSV object never has a worksheet - ever. Rethink your approach to the problem; you're current theory will not work as designed.
    – skub
    Jan 18, 2012 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .