【C#】C#使用Microsoft.Office.Interop.Excel操作Excel表格

Microsoft.Office.Interop.Excel.dll

下载

链接:https://pan.baidu.com/s/1uPEM1MWiaBy2A9TYfhApJA 
提取码:4irq 
 

操作

 

应用程序

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

工作簿

 Workbook wbk = app.Workbooks.Open(tbFilePath.Text);

Workbooks wbks = app.Workbooks;
Workbook wbk = wbks.Add(tbFilePath.Text);

工作表

Worksheet wsh = wbk.Sheets["All"];

Sheets shs = wbk.Sheets;
Worksheet wsh = (Worksheet)shs.get_Item(1);

读取

string str = wsh.Cells[1, 1].Value.ToString();

写入(索引以1开始)

wsh.Cells[2, 1] = "str";

保存

 wbk.Save();

wbk.Close(true, null, null);

另存

wbk.SaveAs(tbFilePath.Text, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

退出

app.Quit();

释放

System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

 

一个完整的简洁的读写操作

//应用程序
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
//工作簿
Workbook wbk = app.Workbooks.Open(tbFilePath.Text);
//工作表
Worksheet wsh = wbk.Sheets["All"];

//读取
string str = wsh.Cells[1, 1].Value.ToString();

//写入,索引以1开始
wsh.Cells[2, 1] = "str";

//保存
wbk.Save();

//退出
app.Quit();
//释放
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

 

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页