OpenXML SDKを利用してExcelファイルを読み込む方法はこちらの記事を参照してください。
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 Microsoft.Office.Interop.Excel;
namespace ExcelFileRead
{
public partial class FormSimpleRead : Form
{
public FormSimpleRead()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Visible = false;
Workbook wb = ExcelApp.Workbooks.Open(openFileDialog1.FileName,
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);
Worksheet ws1 = wb.Sheets[1];
ws1.Select(Type.Missing);
Range range = ExcelApp.get_Range("A1", Type.Missing);
if (range != null) {
var val = range.Value2;
textBox1.Text += Convert.ToString(val);
}
wb.Close(false, Type.Missing, Type.Missing);
ExcelApp.Quit();
}
}
}
}
using Microsoft.Office.Interop.Excel;
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Visible = false;
Workbook wb = ExcelApp.Workbooks.Open(openFileDialog1.FileName,
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);
Worksheet ws1 = wb.Sheets[1];
ws1.Select(Type.Missing);
Range range = ExcelApp.get_Range("A1", Type.Missing);
if (range != null) {
var val = range.Value2;
textBox1.Text += Convert.ToString(val);
}
wb.Close(false, Type.Missing, Type.Missing);
ExcelApp.Quit();
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 Microsoft.Office.Interop.Excel;
namespace ExcelFileRead
{
public partial class FormSimpleRead : Form
{
public FormSimpleRead()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Visible = false;
Workbook wb = ExcelApp.Workbooks.Open(openFileDialog1.FileName,
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);
Worksheet ws1 = wb.Sheets[1];
ws1.Select(Type.Missing);
Range range = ExcelApp.get_Range("A1", Type.Missing);
if (range != null) {
var val = range.Value2;
textBox1.Text += Convert.ToString(val);
}
wb.Close(false, Type.Missing, Type.Missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws1);
ws1 = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp.Workbooks);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
ExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
ExcelApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
}
}
ReleaseComObject()
を呼び出して開放します。
変数にオブジェクトを代入していた場合は、解放後に変数にnullを代入し、変数から参照されていない状態にします。 System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws1);
ws1 = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp.Workbooks);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
ExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
ExcelApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();