EXCEL中的格式是这样的:
A B C D
1 2 3 4
12 3 4 23
要在C#程序中建立一个结构数组
将以上信息读到结构数组中
EXCEL中的格式是这样的:
A B C D
1 2 3 4
12 3 4 23
要在C#程序中建立一个结构数组
将以上信息读到结构数组中
using System;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace CExcel1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int[,]arraynumber = new int[4,4];
Excel.Application xApp=new Excel.ApplicationClass();
xApp.Visible=true;
Excel.Workbook xBook=xApp.Workbooks._Open(@"D:\Sample.xls",
Missing.Value,Missing.Value,Missing.Value,Missing.Value
,Missing.Value,Missing.Value,Missing.Value,Missing.Value
,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
Excel.Worksheet xSheet=(Excel.Worksheet)xBook.Sheets[1];
for (int row = 0;row<4;++row)
{
for (int col = 0;col<4;++col)
{
Excel.Range rng2=(Excel.Range)xSheet.Cells[row,col];
arraynumber[row,col]=Convert.ToInt32(rng2.Value2.ToString());
}
}
}
}
}
如果找不到“Microsoft.Office.Interop.Excel;”请打补丁“ http://www.microsoft.com/downloads/thankyou.aspx?familyId=3c9a983a-ac14-4125-8ba0-d36d67e0f4ad&displayLang=en”并且确认项目已经引用了“Excel object 11.0”。
在组件里引用office的excel的dll
下面这个方法很强大,但是用起来也通过一个一个的读
private string _connectString = " Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source={0} ";
/// <summary> /// 读取Excel某个单元的数据 /// </summary> /// <param name="sheetname"></param> /// <param name="path"></param> /// <param name="xcell"></param> /// <param name="ycell"></param> /// <returns></returns> public string ReaderCellData(string sheetname, string path, int xcell, int ycell) { string val = ""; Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass(); app.Visible = false;
Microsoft.Office.Interop.Excel.WorkbookClass workbook = (Microsoft.Office.Interop.Excel.WorkbookClass)app.Workbooks.Open(path, //Environment.CurrentDirectory+ Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
object missing = Type.Missing; Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets; Microsoft.Office.Interop.Excel.Worksheet datasheet = null; foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets) //取出指定的sheet { if (sheet.Name == sheetname) { datasheet = sheet; break; } }
//取得某个单元格xcell, ycell Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)datasheet.Cells[xcell, ycell]; val = range.Value2.ToString();
app.Quit(); app = null;
return val; }
/// <summary> /// 根据列名和工作表Sheet名读取Excel /// /// </summary> /// <param name="fullPath">excel的绝对路径</param> /// <param name="cols">读取excel的列</param> /// <param name="sheetName">工作表名sheet</param> /// <returns>DataTable</returns> public DataTable ReadExcelDataByCol(string fullPath,string []cols,string sheetName) { //string xlsPath = Server.MapPath("~/app_data/somefile.xls"); // 绝对物理路径 //data source为文件的绝对路径 string connStr = string.Format(_connectString, fullPath); string colStr = "";
//合成所查询的column for (int i=0; i<cols.Length;i++) { if (i == cols.Length - 1) { colStr += cols[i]; } else { colStr += string.Format(" {0},",cols[i]); } } // 查询语句 string sql = string.Format("SELECt {0} FROM [{1}$]",colStr,sheetName);
DataSet ds = new DataSet(); OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr); da.Fill(ds); // 填充DataSet
return ds.Tables[0]; }
这东西很简单,引用office的excel的dll
然后打开这个excel文件,就跟读取一个二维表一样容易。具体代码就不写了 百度一搜一大堆。