C# 操作Excel并读取数据
publicstaticvoidLoadStuFromEls(stringxlsFileName,refArrayListarrtStu){MsExcel.Applica...
public static void LoadStuFromEls(string xlsFileName,ref ArrayList arrtStu) { MsExcel.Application oExcApp; MsExcel.Workbook oExcBook;
oExcApp = new MsExcel.ApplicationClass(); oExcBook = oExcApp.Workbooks.Open(xlsFileName, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); MsExcel.Worksheet worksheet1 = (MsExcel.Worksheet)oExcBook.Worksheets["sheet1"]; worksheet1.Activate(); Student oneStu; MsExcel.Range range1; int i = 2; range1 = worksheet1.get_Range(string.Format("A{0}",i)); while (range1.Text.ToString().Length > 0) { oneStu = new Student(); range1 = worksheet1.get_Range(string.Format("A{0}", i)); oneStu.StuName = range1.Text.ToString(); range1 = worksheet1.get_Range(string.Format("D{0}", i)); oneStu.roomNum = range1.Text.ToString(); range1 = worksheet1.get_Range(string.Format("E{0}", i)); oneStu.PhoneNum = range1.Text.ToString(); arrtStu.Add(oneStu); i++; range1 = worksheet1.get_Range(string.Format("A{0}", i));
}}
书上看到的,照着打代码都是有问题的, worksheet1.get_Range();知道要两个参数,但不知道是什么?以上代码好像是读取每一个学生的信息,但行列数据那里具体怎么获取的?望指教,谢谢! 展开
oExcApp = new MsExcel.ApplicationClass(); oExcBook = oExcApp.Workbooks.Open(xlsFileName, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); MsExcel.Worksheet worksheet1 = (MsExcel.Worksheet)oExcBook.Worksheets["sheet1"]; worksheet1.Activate(); Student oneStu; MsExcel.Range range1; int i = 2; range1 = worksheet1.get_Range(string.Format("A{0}",i)); while (range1.Text.ToString().Length > 0) { oneStu = new Student(); range1 = worksheet1.get_Range(string.Format("A{0}", i)); oneStu.StuName = range1.Text.ToString(); range1 = worksheet1.get_Range(string.Format("D{0}", i)); oneStu.roomNum = range1.Text.ToString(); range1 = worksheet1.get_Range(string.Format("E{0}", i)); oneStu.PhoneNum = range1.Text.ToString(); arrtStu.Add(oneStu); i++; range1 = worksheet1.get_Range(string.Format("A{0}", i));
}}
书上看到的,照着打代码都是有问题的, worksheet1.get_Range();知道要两个参数,但不知道是什么?以上代码好像是读取每一个学生的信息,但行列数据那里具体怎么获取的?望指教,谢谢! 展开
3个回答
展开全部
获得 选择的 range,需要知道两个参数,比如:A1到B2,这样才能确定一个选择范围
get_Range 函数需要两个参数,而你上面代码只有一个参数。
参考:http://blog.sina.com.cn/s/blog_5d93d7aa0101d9jx.html
get_Range 函数需要两个参数,而你上面代码只有一个参数。
参考:http://blog.sina.com.cn/s/blog_5d93d7aa0101d9jx.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
获取相应单元格的数据:worksheet1.get_Range(string.Format("A{0}", i), Missing.Value).Value2。只看了代码的错误地方,你把你的代码替换成这个试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询