c# 如何打开excel文件

2024年11月22日 13:44
有4个网友回答
网友(1):

string excelName= ExcelFileUrl();//返回Excel的路径
string fileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + excelName;//保存Excle的文件路径
object missing = Missing.Value;
Microsoft.Office.Interop.Excel.Application appExcel = null;//实例Excel类

appExcel = new Microsoft.Office.Interop.Excel.Application();
appExcel.DisplayAlerts = false;//DisplayAlerts 属性设置成 False,就不会出现这种警告。
Microsoft.Office.Interop.Excel.Workbook workbook = appExcel.Workbooks.Open(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, Type.Missing);//打开Excel
//Microsoft.Office.Interop.Excel.Workbooks workbooks = appExcel.Workbooks;//
//Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(missing);//工作簿
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets;//实例表格
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets[1];//第一个表格

网友(2):

引用
Microsoft.Office.Interop.Excel.dll

网友(3):

安装WPS或OFFICE即可打开

网友(4):