以前自己写的一篇日志,希望对你有帮助!
读取:将Excel中的数据读入到 DataSet 中
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
class ExcelReader
{
private DataSet myData;
public ExcelReader(String Path)
{
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strConn);
String strCom = "SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
myData = new DataSet();
myCommand.Fill(myData, "[Sheet1$]");
myConn.Close();
}
public DataSet GetData()
{
return myData;
}
}
剩下的就是对 DataSet 的操作了,可根据需要进行读取。
写入:假设要写入的数据存在 String[][] myData 中
using System;
using System.Collections.Generic;
using System.Text;
class ExcelWriter
{
public ExcelWriter(String[][] myData, String Path)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
for (int i = 0; i < myData.Length; i++)
{
String[] DataRow = myData[i];
for (int j = 0; j < DataRow.Length; j++)
{
excel.Cells[i + 1, j + 1] = DataRow[j]; //这里可换成其他数据类型
}
}
excel.DisplayAlerts = false;
workbook.Save();
excel.Save(Path);
excel.Quit();
}
}
LS代码不错
不过只读的话,没必要这么复杂
public void shuchu()
{
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\excell.xls;Extended Properties=Excel 8.0;");
DataSet ds = new DataSet();
string TJ = "SELECT * FROM [myRange1$]";
OleDbDataAdapter da = new OleDbDataAdapter(TJ, objConn);
try
{
da.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
objConn.Close();
return;
}
biao = ds.Tables[0];//将excel表中的数据读入到内存表biao中,以备后用。
objConn.Close();
}//连接并打开数据库,将数据导入内存表biao中,myRangw1为excel中工作表名。biao做查询使用。
public void chaxun(string cha)
{
dv = biao.DefaultView;
dv.RowFilter = cha;
temp = dv.ToTable();
dataGridView1.DataSource = temp;
}//cha为查询条件,将cha传递给chaxun函数以便进行查询。
这是我的代码
///
/// DataSet导出数据到Excel
///
/// DataSet对象
///
public static bool DataSet2Excel(DataSet ds)
{
try
{
Excel.Application myExcel = new Excel.Application();
myExcel.Application.Workbooks.Add(true);
myExcel.Visible = true;
//生成标题
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
myExcel.Cells[1, i + 1] = ds.Tables[0].Columns[i].Caption;
}
//填充数据
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
myExcel.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
return true;
}
catch
{
return false;
}
}
我这有断代码自己用的行,里面具体的内容你改改!希望能帮到你
#region public bool SaveExcel(DataGridView paramGirdView) 保存datagridview 数据到 Excel
///
/// 保存datagridview 数据到 Excel
///
/// datagridview
///
public bool SaveExcel(DataGridView paramGirdView)
{
if (paramGirdView.Rows.Count == 0) //判断数据是否等于0
{
return false;
}
//创建 Excel 对象
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
myExcel.Application.Workbooks.Add(true);
myExcel.Visible = true;
myExcel.Cells[1, 2] = "学生基本信息";
//生成字段名称(列名)
for (int i = 0; i < paramGirdView.ColumnCount; i++)
{
// 第二行第二列开始录入数据
myExcel.Cells[2, i + 2] = paramGirdView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < paramGirdView.RowCount; i++)
{
for (int j = 0; j < paramGirdView.ColumnCount; j++)
{
//判断类型是否是字符串
if (paramGirdView[j, i].ValueType == typeof(string))
{
// 第二行第二列开始录入数据
if (paramGirdView[j, i].Value != null)
{
myExcel.Cells[i + 3, j + 2] = "'" + paramGirdView[j, i].Value.ToString();
}
}
else
{
if (paramGirdView[j, i].Value != null)
{
myExcel.Cells[i + 3, j + 2] = paramGirdView[j, i].Value;
}
}
}
}
return true;
}
#endregion