C# winform 中如何导入Excel
1个回答
展开全部
你是要从excel中导入数据到winform吗?如果是这样,可以这样:引用office11.0组件后, Microsoft.Office.Interop.Excel.Application application; //这是一个客户端
Microsoft.Office.Interop.Excel.Workbooks workbooks; //所有工作薄
Microsoft.Office.Interop.Excel.Worksheet worksheet;//工作表
Microsoft.Office.Interop.Excel.Workbook workbook; //所用到的工作表 void IsRunEX(){ OpenFileDialog openfilediaglog = new OpenFileDialog();
openfilediaglog.Filter = "xls文件|*.xls";
if (openfilediaglog.ShowDialog() == DialogResult.OK){FieldName = openfilediaglog.FileName;
application = new Microsoft.Office.Interop.Excel.Application();
workbooks = application.Workbooks;
workbook = returnworkbook(FieldName, workbooks);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; //选择第一个表
Range range = worksheet.Cells[1, 8] as Range; //这是选择第一行第八列的内容
Range rangee = worksheet.Cells[1, 9] as Range; //这是第一行到九列的内容 string str1=range.Value2.ToString(); string str2=ragee.Value2.ToString(); ................... //你所要做的操作 .................. workbook.Close(Type.Missing, FieldName, Type.Missing);
workbooks.Close(); //退出关闭资源
application.Quit();}} private Workbook returnworkbook(string filename,Workbooks works) //这里是打一开一个工作表{Microsoft.Office.Interop.Excel.Workbook wk=works.Open(
filename, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.Workbooks workbooks; //所有工作薄
Microsoft.Office.Interop.Excel.Worksheet worksheet;//工作表
Microsoft.Office.Interop.Excel.Workbook workbook; //所用到的工作表 void IsRunEX(){ OpenFileDialog openfilediaglog = new OpenFileDialog();
openfilediaglog.Filter = "xls文件|*.xls";
if (openfilediaglog.ShowDialog() == DialogResult.OK){FieldName = openfilediaglog.FileName;
application = new Microsoft.Office.Interop.Excel.Application();
workbooks = application.Workbooks;
workbook = returnworkbook(FieldName, workbooks);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; //选择第一个表
Range range = worksheet.Cells[1, 8] as Range; //这是选择第一行第八列的内容
Range rangee = worksheet.Cells[1, 9] as Range; //这是第一行到九列的内容 string str1=range.Value2.ToString(); string str2=ragee.Value2.ToString(); ................... //你所要做的操作 .................. workbook.Close(Type.Missing, FieldName, Type.Missing);
workbooks.Close(); //退出关闭资源
application.Quit();}} private Workbook returnworkbook(string filename,Workbooks works) //这里是打一开一个工作表{Microsoft.Office.Interop.Excel.Workbook wk=works.Open(
filename, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |