Microsoft.Office.Interop.Excel.dll

download

Link: pan.baidu.com/s/1uPEM1MWi… Extraction code: 4IRQ

operation

The application

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

workbook

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

or

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

The worksheet

Worksheet wsh = wbk.Sheets["All"];
Copy the code

or

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

read

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

Write (index starts with 1)

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

save

 wbk.Save();
Copy the code

or

wbk.Close(true, null, null);
Copy the code

save

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);
Copy the code

exit

app.Quit();
Copy the code

The release of

System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
Copy the code

A complete and concise read and write operation

/ / Application. Microsoft Office. Interop. Excel. Application app = new Microsoft. Office. Interop. Excel. The Application (); // Workbook WBK = app.workbooks.open (tbFilePath.Text); WSH = wbk.Sheets["All"]; String STR = wsh.cells [1, 1].value.toString (); Wsh.cells [2, 1] = "STR "; wsh.cells [2, 1] =" STR "; / / Save WBK. The Save (); / / exit the app. The Quit (); / / release System. The Runtime. InteropServices. Marshal. ReleaseComObject (app);Copy the code