日期:2014-05-18  浏览次数:20834 次

excel
我想读取excel 里面的数据。第一次用啊 ,不知道怎么用, 那位能指点指点?有例子的发个例子啊。


------解决方案--------------------
操作EXCEL跟操作其他数据库一样,EXCEL本来也就是一种数据库。
------解决方案--------------------
帮LZ顶
------解决方案--------------------
要引入excel操作的三个dll文件,自己下,这是我的代码,参考一下
using System;
using System.Collections.Generic;
using System.Text;

namespace ResourceManage
{
public class ExcelData
{
private List<SaveData> QueryData = null;

private Excel.ApplicationClass MyExcel = null;
private Excel.Workbooks MyWorkBooks = null;
private Excel.Workbook MyWorkBook = null;
private Excel.Worksheet MyWorkSheet = null;

public ExcelData(string FileName)
{
QueryData = new List<SaveData>();

MyExcel = new Excel.ApplicationClass();
MyWorkBooks = MyExcel.Workbooks;
MyWorkBook = MyWorkBooks.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);
MyWorkSheet = (Excel.Worksheet)MyWorkBook.Worksheets[1];
}

// 查询 
public List<SaveData> ExcelQueryData()
{
for (int i = 2; i <= MyWorkSheet.UsedRange.Rows.Count; i++)
{
SaveData tempdata = new SaveData();
tempdata.id = ((Excel.Range)MyWorkSheet.Cells[i, 1]).Text.ToString();
tempdata.CodeName = ((Excel.Range)MyWorkSheet.Cells[i, 2]).Text.ToString();
tempdata.Language = ((Excel.Range)MyWorkSheet.Cells[i, 3]).Text.ToString();
tempdata.FileName = ((Excel.Range)MyWorkSheet.Cells[i, 4]).Text.ToString();
QueryData.Add(tempdata);
}
return QueryData;
}

// EXCEL数据增加
public void ExcelAddData(SaveData addData)
{
int count = MyWorkSheet.UsedRange.Rows.Count + 1;
Excel.Range addRange = MyWorkSheet.get_Range(MyWorkSheet.Cells[count, 1], MyWorkSheet.Cells[count, 4]);
object[] obj ={ addData.id, addData.CodeName, addData.Language, addData.FileName };
addRange.Value2 = obj;
// 保存
MyWorkBook.Save();
}

// EXCEL数据修改
public void ExcelUpdateData(SaveData updateData)
{

for (int i = 2; i <= MyWorkSheet.UsedRange.Rows.Count; i++)
{
string tempId = ((Excel.Range)MyWorkSheet.Cells[i, 1]).Text.ToString();
if (string.Compare(updateData.id, tempId) == 0)
{
Excel.Range updateRange = MyWorkSheet.get_Range(MyWorkSheet.Cells[i, 1], MyWorkSheet.Cells[i, 4]);
object[] obj ={ updateData.id, updateData.CodeName, updateData.Language, updateData.FileName };
updateRange.Value2 = obj;
// 保存
MyWorkBook.Save();
break;
}
}
}

// EXCEL 数据删除
public void ExcelDeleteData(string delID)
{
for (int i = 2; i <= MyWorkSheet.UsedRange.Rows.Count; i++)
{
string tempId = ((Excel.Range)MyWorkSheet.Cells[i, 1]).Text.ToString();
if (string.Compare(delID, tempId) == 0)
{
Excel.Range delRange = MyWorkSheet.get_Ran