Excel导入---后台下载
2016-07-27 16:24
811 查看
/// <summary>
/// 根据路径读取Excel放入DataTable里
/// </summary>
/// <param name="path"></param>
/// <param name="error"></param>
/// <returns></returns>
public DataTable ExcelToDS(string path,out string error)
{
string fileType = System.IO.Path.GetExtension(path);
string fileType1 = path.Substring(path.LastIndexOf('.'));
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;";
if (fileType == ".xls")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
DataSet ds = null;
try
{
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
//得到所有sheet的名字
DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
//得到第一个sheet的名字
string firstSheetName = sheetsName.Rows[0][2].ToString();
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
error = "";
}
catch(Exception ex)
{
error = ex.Message;
}
return ds.Tables[0];
}
/// <summary>
/// 下载文件到指定路径
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public string Download(string filePath)
{
string path = fileP + Path.GetFileName(filePath);
// 设置参数
HttpWebRequest request = WebRequest.Create(filePath) as HttpWebRequest;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建本地文件写入流
Stream stream = new FileStream(path, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
return path;
}
/// 根据路径读取Excel放入DataTable里
/// </summary>
/// <param name="path"></param>
/// <param name="error"></param>
/// <returns></returns>
public DataTable ExcelToDS(string path,out string error)
{
string fileType = System.IO.Path.GetExtension(path);
string fileType1 = path.Substring(path.LastIndexOf('.'));
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;";
if (fileType == ".xls")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
DataSet ds = null;
try
{
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
//得到所有sheet的名字
DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
//得到第一个sheet的名字
string firstSheetName = sheetsName.Rows[0][2].ToString();
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
error = "";
}
catch(Exception ex)
{
error = ex.Message;
}
return ds.Tables[0];
}
/// <summary>
/// 下载文件到指定路径
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public string Download(string filePath)
{
string path = fileP + Path.GetFileName(filePath);
// 设置参数
HttpWebRequest request = WebRequest.Create(filePath) as HttpWebRequest;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建本地文件写入流
Stream stream = new FileStream(path, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
return path;
}
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 使用Python生成Excel格式的图片
- c#调用COM组件
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- VBA将excel数据表生成JSON文件
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- 星外ASP.Net的安全设置相关说明
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性