问题描述
我想转换一个Excel文件CSV和我写此C#代码:
Excel.Application excelApp =新的Excel .ApplicationClass();
Excel.Workbooks工作簿= excelApp.Workbooks;
excelApp.DisplayAlerts = FALSE;
Excel.Workbook工作簿= workBooks.Open(的资源文件,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing ;,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing);
workBook.SaveAs(csvFilePath,Excel.XlFileFormat.xlCSV,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing);
workBook.Close(假的资源文件,NULL);
excelApp.DisplayAlerts = TRUE;
Excel.Application应用= excelApp.Application;
app.Quit();
对Marshal.ReleaseComObject(练习册);
对Marshal.ReleaseComObject(练习册);
对Marshal.ReleaseComObject(excelApp);
对Marshal.ReleaseComObject(应用程序);
的资源文件= csvFilePath;
但CSV文件分隔用逗号。我已经在互联网上搜索。我试着地区设置和其他东西,但他们没有解决我的问题。
输出示例: Hesap,391,TL ,,,
我想这样的输出: Hesap; 391; TL ;;;
我有同样的问题 - 我有一个项目从Windows Server 2003迁移到Windows Server 2012,而这一次
workBook.SaveAs(csvFilePath,Excel.XlFileFormat.xlCSV,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange ,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
和CSV文件是用逗号而不是分号隔开。
我找到了一个解决方案,帮助我避免这个问题。
格式:
oBook.SaveAs(newFileName,XlFileFormat.xlUnicodeText,
失踪,失踪,假的,假的,
XlSaveAsAccessMode.xlNoChange,
失踪,失踪,失踪,失踪,失踪) ;
和之后,我重写文件是这样的:
字符串文本= File.ReadAllText(newFileName);
文本= text.Replace(\t,;)。更换({TAB},\t);
File.WriteAllText(newFileName,文字,Encoding.UTF8);
所以,我必须用分号分隔CSV文件。
,而另一个问题左转 - 带日期和个行业的浮点值有另一种格式, - 日期是2015年10月5日18:54,而不是2015年5月10日18:54和浮点值5.55,而不是5,55 。 p>
我觉得最后两个问题,我会在我的Oracle过程转换vaules决定,但主要问题是过去了 - 我们有我们需要的相同的csv文件。
有些开发者可能假设逗号是正确的,分号没有或不是这些delemiters之间的差异,但如果你有一个大的项目有在服务器端的所有程序在一个工作方式是很难设置和纠正他们。
和我不明白一件事 - 如果我有在Excel文件中的字段一个逗号和我用这个方法workbook.SaveAs,我们将如何利用正确的值领域的进一步吗? - 将有两个值,而不是之一!随着分号的情况是一样的,但我们从来没有在我们的Excel文件使用分号。
I want to convert an Excel file to csv and I wrote this C# code:
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbooks workBooks = excelApp.Workbooks;
excelApp.DisplayAlerts = false;
Excel.Workbook workBook = workBooks.Open(sourceFile,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, ';',
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
workBook.SaveAs(csvFilePath, Excel.XlFileFormat.xlCSV, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workBook.Close(false, sourceFile, null);
excelApp.DisplayAlerts = true;
Excel.Application app = excelApp.Application;
app.Quit();
Marshal.ReleaseComObject(workBooks);
Marshal.ReleaseComObject(workBook);
Marshal.ReleaseComObject(excelApp);
Marshal.ReleaseComObject(app);
sourceFile = csvFilePath;
But the csv file is seperated with commas. I have searched on the internet. I tried Region Setting and other things, but none of them solved my problem.
Example output: Hesap,391,TL,,,
I want this output: Hesap;391;TL;;;
I had the same problem, - I had to migrate one project from Windows Server 2003 to Windows Server 2012, and this one
workBook.SaveAs(csvFilePath, Excel.XlFileFormat.xlCSV, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
and csv file was separated with commas instead of semicolons.I found one solution that helped me to avoid this problem
Formatting:
oBook.SaveAs(newFileName, XlFileFormat.xlUnicodeText,
missing, missing, false, false,
XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
And after I rewrite file this way:
string text = File.ReadAllText(newFileName);
text = text.Replace("\t", ";").Replace("{tab}", "\t");
File.WriteAllText(newFileName, text, Encoding.UTF8);
So I have csv file separated by semicolons.But another problem left - fileds with date and float values have another format, - date is 10/5/2015 18:54 instead of 5.10.2015 18:54 and float values 5.55 instead of 5,55.
I think the last two problems i will decide by converting vaules in my oracle procedures, but the main problem is passed - we have the same csv file we need.
Some of developers may suppose that comma is right and semicolon not or it is not difference between these delemiters, but if you have a big project there all procedures on the server side work in one way it is difficult to set and correct all of them.
And I don't understand one thing - if i have a comma in the field in excel file and i use this method workbook.SaveAs, how we will take correct value of the field further? - there will be two values instead of one! With semicolon situation is the same, but we never use semicolons in our Excel files.
这篇关于C#导出CSV用分号文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!