Execl如何导入导出到DataSet ?

asd432 2008-12-03 12:25:58
请问Execl导入导出到DataSet 最好有代码
...全文
84 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
showmy 2008-12-03
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Excel;
using System.Data.OleDb;

public partial class index : System.Web.UI.Page
{
private const string strConn = "连接字符串";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataToGridView();
}
}

/// <summary>
/// 绑定
/// </summary>
private void BindDataToGridView()
{
SqlConnection conn = new SqlConnection(strConn);
string strSQL = "select * from [User]";
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
da.Fill(ds, "[User]");
this.GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "UserId" };
this.GridView1.DataBind();
}

/// <summary>
/// 导出DataSet到Execl
/// </summary>
private void OutExecl()
{
Excel.Application myExcel = new Excel.Application();
myExcel.Visible = true;

if (myExcel == null)
{
Page.RegisterStartupScript("", "<script>alert('EXCEL无法启动');</script>");
}

Workbook work = myExcel.Application.Workbooks.Add(Type.Missing);
Worksheet sheet = (Worksheet)work.Worksheets[1];

int rowCount = 0;
int columnCount = 0;

columnCount = this.GridView1.Columns.Count;
rowCount = this.GridView1.Rows.Count;

rowCount--;

for (int m = 1; m < columnCount; m++)
{
sheet.Cells[1, m] = this.GridView1.Columns[m].HeaderText;//得到列标题文本
}

for (int i = 0; i <= rowCount; i++) //二维表填充从每一行开始
{
for (int j = 1; j < columnCount; j++) //填充每一行第j列单元格,循环添加行列数据
{
sheet.Cells[i + 2, j] = this.GridView1.Rows[i].Cells[j].Text;
}
}
Page.RegisterStartupScript("", "<script>alert('成功导出!');</script>");
}

/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)
{
OutExecl();
}

/// <summary>
/// 导入Excel到DataSet
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private DataSet InExecl(string filePath)
{
DataSet ds = new DataSet();
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";

OleDbConnection myConn = new OleDbConnection(connStr);
string strSQL = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strSQL, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[Sheet1$]");
myConn.Close();

return myDataSet;
}

/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnIn_Click(object sender, EventArgs e)
{
DataSet ds = InExecl(this.File1.PostedFile.FileName);
this.GridView2.DataSource = ds;
this.GridView2.DataBind();
}
}

网上有 搜索下吧 这个应该可以用的
jinjazz 2008-12-03
  • 打赏
  • 举报
回复
用oledb读取excel文件到dataset

http://blog.csdn.net/jinjazz/archive/2007/12/11/1930455.aspx

public static DataSet ExcelToDS(string Pathm,string TableName)
{
DataSet ds = new DataSet();
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]",TableName);
myCommand = new OleDbDataAdapter(strExcel, strConn);

myCommand.Fill(ds, TableName);
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return ds;
}
拉达曼迪斯II 2008-12-03
  • 打赏
  • 举报
回复
有两种方式,一种用上面的传统的ADO。NET读写数据库的方式。
第二种用Linq to Excel, 一个人写的LINQ扩展。
samying 2008-12-03
  • 打赏
  • 举报
回复
我这里写了通用的导入导出类,可以联系我帮你解觉
laoyingisme 2008-12-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/laoyingisme/archive/2007/12/28/1999020.aspx

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧