[問題] C# 寫入Excel檔案Office驗證有問題

作者: DJYA ( )   2016-01-28 22:00:51
Hi 各位大大,
我用Microsoft.Office.Interop.Excel 寫一個把值寫進去.xls檔案的程式,
寫值進去是成功的,沒有拋出任何exception,
但是在少數電腦上寫出來的檔同樣用Microsoft.Office.Interop.Excel開啟的時候
(用excel開啟也會有警告)
會拋出以下exception,
"Office偵測到此檔案有問題,為保護您的電腦,此檔案無法開啟"
請問一下,是不是我程式哪裡有問題,
還是該台電腦有什麼設定需要調整?
以下是我的程式碼片段,有設定替excel加入防寫密碼,
並在檔案存檔後,把其屬性改為唯讀。
// Modify file.
{
System.IO.File.SetAttributes(strFilename,
System.IO.FileAttributes.Normal);
Excel.Application App = new Excel.Application();
App.Visible = false;
App.UserControl = false;
Excel.Workbook Wbook = App.Workbooks.Open(strFilename,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, passwd);
Excel.Worksheet Wsheet = (Excel.Worksheet)Wbook.Sheets[1];
Wsheet.Application.DisplayAlerts = false;
Wsheet.Application.AlertBeforeOverwriting = false;
//要寫入的內容放在this.ListRusult裡面
App.Cells[1, col] = this.FirstTime;
int num = this.listResult.Count();
for (int i = 0; i < num; ++i)
{
App.Cells[i + 2, col] = this.listResult[i];
}
Wbook.Save();
Wbook.Close();
App.Quit();
System.IO.File.SetAttributes(strFilename,
System.IO.FileAttributes.ReadOnly);
this.PrintMessage("Output to " + strFilename);
}
作者: johnpage (johnpage)   2016-01-29 17:19:00
Office版本????
作者: andy210365 (chih)   2016-01-30 00:04:00
office 版本問題
作者: DJYA ( )   2016-01-30 09:45:00
請問是什麼問題啊,會當的跟不會當的都是2010耶
作者: CrazyAngel (無口系天然呆)   2016-01-30 17:10:00
https://support.microsoft.com/zh-tw/kb/973736或許跟這個有關,試試看

Links booklink

Contact Us: admin [ a t ] ucptt.com