[問題] C# 寫入Excel檔案Office驗證有問題
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);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.228.44.133
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1453989653.A.575.html
※ 編輯: DJYA (61.228.44.133), 01/28/2016 22:09:05
推
01/29 17:19, , 1F
01/29 17:19, 1F
推
01/30 00:04, , 2F
01/30 00:04, 2F
→
01/30 09:45, , 3F
01/30 09:45, 3F
推
01/30 17:10, , 4F
01/30 17:10, 4F
→
01/30 17:10, , 5F
01/30 17:10, 5F
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章