Microsoft.Office.Interop.Excel.Application SaveAs() method does not
preserve encoding
I'm having an issue saving excel files as CSV's and preserving the
encoding in C#. Specifically, the following takes an excel file and saves
it as a csv file with the same name, which is passed to the loadCsvData
function, whose job is to insert the data into a database in an organized
fashion. My issue is that the excel file contains fractional symbols which
are not recognized in a csv format, unless i MANUALLY open each .xml and
save as csv (for some reason this works while the
Microsoft.Office.Interop.Excel.Application method SaveAs() does not):
List<string[]> data = new List<string[]>();
string fileName = (date.ToString("MM/dd/yyyy") +
".csv").Replace("/","");
string excelFileName = (date.ToString("MM/dd/yyyy") +
".xml").Replace("/","");
System.Console.WriteLine("Processing file: " + fileName);
System.Console.ReadLine();
// Open excel and save as csv. Then process the data
Microsoft.Office.Interop.Excel.Application app = new
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook =
app.Workbooks.Open(@"C:\Users\TAG\Desktop\TIP holdings\" +
excelFileName);
workBook.SaveAs(@"C:\Users\TAG\Desktop\TIP holdings\CSV Data\"
+ fileName,
Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV);
workBook.Close(false,"",true);
data = loadCsvData(fileName);
insertData(data);
No comments:
Post a Comment