使用環境はwindows10、Visual Studio2019(Windows Forms アプリ、.NET Framework、バージョン4.8)です。
こちらのサイトとほぼ同じコードを入力し「開始」を押しました。ですがthrow;の右に赤い×印が出て次のようなエラー?が出ます。
System.Runtime.InteropServices.COMException: 'ファイルにアクセスできませんでした。次のいずれかを確認してみてください: • 指定したフォルダーが存在することを確認します。 • ファイルが保存されているフォルダーが読み取り専用でないことを確認します。 • ファイル名とフォルダーのパスに次の文字が含まれていないことを確認します: < > ? [ ] : | * • ファイル名とフォルダーのパスが 218 文字以下になっていることを確認します。'
以下入力したコードです。解決方法を教えてください。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace _20211210 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Excel.Application excelApp = new Excel.Application(); Excel.Workbooks excelBooks = excelApp.Workbooks; Excel.Workbook excelBook = excelBooks.Add(); Excel.Worksheet sheet = excelApp.Worksheets["sheet1"]; try { excelApp.Visible = false; sheet.Cells[1, 1] = "名前"; excelBook.SaveAs("C:\test"); } catch { throw; } finally { excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } } } }
あなたの回答
tips
プレビュー