概述
- Excel.Application ea = new Excel.ApplicationClass();
- ea.Workbooks.Open("c://2.xls", Missing.Value, Excel.XlFileAccess.xlReadWrite, Missing.Value, Missing.Value,
- Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlEditionType.xlPublisher,
- 2, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
- Excel._Worksheet sh;
- sh = (Excel._Worksheet)ea.Worksheets.get_Item(1);
- DataSet myDs = new DataSet();
- DataTable xlsTable = myDs.Tables.Add("show");
- DataRow dr;
- xlsTable.Columns.Add("DIVCD", typeof(string));
- xlsTable.Columns.Add("DIVName", typeof(string));
- xlsTable.Columns.Add("ItemCD", typeof(string));
- int i = 1;
- while (((Excel.Range)sh.Cells[i, 1]).Text.ToString() != "")
- {
- string s = " ";
- dr = xlsTable.NewRow();
- dr[0] = ((Excel.Range)sh.Cells[i, 1]).Text.ToString();
- s = s + ((Excel.Range)sh.Cells[i, 1]).Text.ToString();
- dr[1] = ((Excel.Range)sh.Cells[i, 2]).Text.ToString();
- s = s + ((Excel.Range)sh.Cells[i, 2]).Text.ToString();
- dr[2] = ((Excel.Range)sh.Cells[i, 3]).Text.ToString();
- s = s + ((Excel.Range)sh.Cells[i, 3]).Text.ToString();
- if (((Excel.Range)sh.Cells[i, 4]).Text.ToString() == "rrrrr")
- {
- //((Excel.Range)sh.Cells[i, 4]).Interior.ColorIndex = 29;
- }
- s = s + ((Excel.Range)sh.Cells[i, 4]).Text.ToString();
- xlsTable.Rows.Add(dr);
- Console.WriteLine(s);
- Console.WriteLine("----------------");
- i++;
- }
- ea.Workbooks.Close();
- ea.Quit();
- System.Runtime.InteropServices.Marshal.ReleaseComObject(ea);
- ea = null;
- System.GC.Collect();
最后
以上就是会撒娇睫毛为你收集整理的C#中如何遍历Excel的全部内容,希望文章能够帮你解决C#中如何遍历Excel所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复