把Excel里的数据转换为DataTable,并返回DataTable:
public static DataTable ExcelToDataTable(string excelPath) //把Excel里的数据转换为DataTable,并返回DataTable
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;IMEX=1'";
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
string strCom = "SELECT * FROM [Sheet1$]";
DataTable dt;
try
{
Conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
Conn.Close();
dt = ds.Tables[0];
}
catch (Exception err)
{
throw err;
}
return dt;
}这个方法如果Excel表是打开的就能获得这个Excel里面的数据,如果Excel没有打开就会在conn.open()那里报错。
报错内容:.Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC)。请安装 Microsoft Data Access Components(MDAC) 2.6 或更高版本。还有一个我上传一个Excel表代码:Webservice 代码:
[WebMethod]
public void GetFileNew(Byte[] b, string fileName)
{
AcceptFile(b, fileName);
}
public void AcceptFile(Byte[] b,string fileName)
{
string path = System.Configuration.ConfigurationManager.AppSettings["Path"]; string vPath = string.Format("{0}/{1}",path,fileName); string finalPath = Server.MapPath(vPath);
FileStream fStream = new FileStream(finalPath, FileMode.Create, FileAccess.Write);
fStream.Write(b, 0,b.Length);
fStream.Close();
}
[WebMethod]
public Byte[] ReadFile(string filepath)
{
return SendFile(filepath);
}
public Byte[] SendFile(string filepath)
{
// public static Byte[] SendFile(string fPath)
FileStream fStream;
fStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
Byte[] b = new Byte[fStream.Length];
fStream.Read(b, 0, b.Length);
fStream.Flush();
fStream.Close();
return b;
}
调用 Webservice代码:
private void button4_Click(object sender, EventArgs e)
{
test2.ExcelServiceSoapClient h3 = new ExcelEnterFrm.test2.ExcelServiceSoapClient();
string filepath=txtFileAddress.Text;
string NewExcel=filepath.Substring(filepath.LastIndexOf("\\")).Replace("\\","");
string file = "" + operaname + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + NewExcel + "";
Byte[] c1=h3.ReadFile(filepath);
h3.GetFileNew(c1, file);
}这个方法能够上传Excel表,但是如果Excel表是打开的,就上传不了,就报错,说表正在使用中。
求教:有没有方法(或者在我这个代码基础上做一下改动)使得Excel表打开或者关闭都能够导入、导出、上传。
public static DataTable ExcelToDataTable(string excelPath) //把Excel里的数据转换为DataTable,并返回DataTable
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;IMEX=1'";
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
string strCom = "SELECT * FROM [Sheet1$]";
DataTable dt;
try
{
Conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
Conn.Close();
dt = ds.Tables[0];
}
catch (Exception err)
{
throw err;
}
return dt;
}这个方法如果Excel表是打开的就能获得这个Excel里面的数据,如果Excel没有打开就会在conn.open()那里报错。
报错内容:.Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC)。请安装 Microsoft Data Access Components(MDAC) 2.6 或更高版本。还有一个我上传一个Excel表代码:Webservice 代码:
[WebMethod]
public void GetFileNew(Byte[] b, string fileName)
{
AcceptFile(b, fileName);
}
public void AcceptFile(Byte[] b,string fileName)
{
string path = System.Configuration.ConfigurationManager.AppSettings["Path"]; string vPath = string.Format("{0}/{1}",path,fileName); string finalPath = Server.MapPath(vPath);
FileStream fStream = new FileStream(finalPath, FileMode.Create, FileAccess.Write);
fStream.Write(b, 0,b.Length);
fStream.Close();
}
[WebMethod]
public Byte[] ReadFile(string filepath)
{
return SendFile(filepath);
}
public Byte[] SendFile(string filepath)
{
// public static Byte[] SendFile(string fPath)
FileStream fStream;
fStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
Byte[] b = new Byte[fStream.Length];
fStream.Read(b, 0, b.Length);
fStream.Flush();
fStream.Close();
return b;
}
调用 Webservice代码:
private void button4_Click(object sender, EventArgs e)
{
test2.ExcelServiceSoapClient h3 = new ExcelEnterFrm.test2.ExcelServiceSoapClient();
string filepath=txtFileAddress.Text;
string NewExcel=filepath.Substring(filepath.LastIndexOf("\\")).Replace("\\","");
string file = "" + operaname + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + NewExcel + "";
Byte[] c1=h3.ReadFile(filepath);
h3.GetFileNew(c1, file);
}这个方法能够上传Excel表,但是如果Excel表是打开的,就上传不了,就报错,说表正在使用中。
求教:有没有方法(或者在我这个代码基础上做一下改动)使得Excel表打开或者关闭都能够导入、导出、上传。
解决方案 »
- webservice如何远程调用
- C#中的数据库操作
- 如何关闭登录主窗体保留子窗体
- 棘手的问题-“WindowsFormsApplication1.Class.DES”的类型初始值设定项引发异常。
- 在c#+mapx中,如何画多个点和线?
- 编码转换问题
- 如何学好C#语言?
- 文件操作及执行效率哪个更高?
- 我想学学重写某个控件, 比如让ComboBox的item 带image , 我现在不知如何下手了. 请高手指点明路, 介绍些这方面的书籍或该看什么方面的资
- iis上部署asp.net网站,访问上传的文件时被拒绝
- c# word对象模型 怎么获取打开的word文档中选中的文字
- C#中线程WaitHandle的waitone(false)这个是怎么衡量的
{
try
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + sExcelPath + ";Extended Properties='Excel 8.0;IMEX=1'";
string excelSelect = string.Format("select * from [Sheet1$]");
DataSet ds = new DataSet();
using (OleDbConnection oleconn = new OleDbConnection(strConn))
{
//oleconn.Open();
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(excelSelect, oleconn);
oleAdapter.Fill(ds);
//oleconn.Close();
}
return ds.Tables[0];
}
catch (OleDbException ex)
{
return null;
throw ex;
}
}
vs2008 sql2005 office2007
NPOI 要钱!!!???