开发者

VSTO Excel SaveAs Unicode Text and Convert to CSV

I would like to save my CSV file as unicode by using Excel. I have some non ASCII characters in my CSV file. I use SaveAs method inside my VSTO Add-In application. As far as I know i开发者_开发技巧t is not possible to select format of the file in SaveAs method as CSV and generate an Unicode file.

After a lot of searches I concluded that only way to get an Unicode file by using SaveAs method is selecting UnicodeText as file format. However this produces unicode file TAB separated.

So what I decide is saving file as unicode text (Tab separated) and then read file and replace all TAB characters by commas to generate my CSV file. But this brings another problem: File is still open by the excel and error of "File is used by another process" arises. I tried to modify file inside ShutDown event of the Add-In, BeforeClose event of the workbook and Disposed event of the VSTO application but none worked.

Is there any way inside VSTO Add-In to tell Excel closing the file?


I know this reply is a bit late, and you might already found the answer by now. But if anybody else come across this, here is a possible solution,

You can use a temporary file to Save as unicode, then re-write the original file by replacing all the tabs with commas without causing any locking issue. Then reload the original .csv file. On reloading, Excel will always set the focus on first cell for text files. So it might be worth restoring to last known active cell afer reload.

You can find a working solution on http://jaimonmathew.wordpress.com/2011/08/23/excel_addin_to_work_with_unicode_csv/ with full source code.

HTH

Jaimon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜