none
Exporting data from .CSV file to Excel RRS feed

  • Question

  • Hi everyone,

    I was trying to import data set to excel via creating a temporary .csv file in between to speed up the process.

    Approach used:

    1. Create a .csv file with the required data.

    2. Open this .csv file using OpenText() function and save it as exel file.

    3. Delete the temp. created .csv file.

    Now the problem is when user opts for export to workbook option in that case i had to keep the newly created workbook open to the user as per my application requirement. But since as per my approach file is still opened in the excel workbook. So it's not allowing me the delete the temporary .csv file for obvious reason.

    Can anyone suggest me another approach to do this. For exporting i had to use .csv option only, using library closedXML is not in the picture here.

    Thanks in advance


    Monday, December 29, 2014 12:15 PM

Answers

  • Hello,

    Sounds like you should open the csv file, read data into a memory container be it a DataTable or a List(Of T) where T is a class with properties representing data in the csv file. Once data is read in, close the csv file and delete it, continue as you were.

    If you do not need to re-create the file again in the same session consider creating the file using DeleteOnClose when (if done this way) creating a file stream i.e.

    private string TempFileName = IO.Path.Combine(Application.StartupPath, "MexcoCustomers.xml");
    private System.IO.FileStream MexicoCreator = new System.IO.FileStream(TempFileName, System.IO.FileMode.Create, System.Security.AccessControl.FileSystemRights.Modify, System.IO.FileShare.None, 8, System.IO.FileOptions.DeleteOnClose);
    When the app closes the file is deleted as per options set in the constructor for creating the stream.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.



    • Edited by KareninstructorMVP Monday, December 29, 2014 1:53 PM
    • Proposed as answer by Kristin Xie Tuesday, December 30, 2014 9:58 AM
    • Marked as answer by Kristin Xie Wednesday, January 7, 2015 4:51 AM
    Monday, December 29, 2014 1:51 PM