【紧急】怎样把Access数据库里面的多个表导出到同一个Excel文件中(要求每个Sheet代表一个表)

gufengsheng 2005-05-15 10:35:42
搜索了很多地方,都找不到解决方案。
...全文
1190 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
gufengsheng 2005-05-24
  • 打赏
  • 举报
回复
我使用里面的例子:
http://www.chinacs.net/archives/2/2005/01/28/195.html
设置<identity impersonate="true"/>之后,为什么Access就无法Open()了。

我做的导入导出是:
导出:只传数据库名过来,就要把所有的数据表导出到同一个Excel文件,并且以数据表名称作为Sheet名。
导入:选择Excel文件后,就把Excel中的所有数据导入到数据库表中(根据不同的Sheet名,导入到相应的数据表),同时不要把只读字段导入到数据表中。

上述二个要求有点过份,但我也没办法,而且Access的GetShemaTable居然无法获得字段的数据类型和自动编号或只读字段,真是无语。
Cry_Out 2005-05-24
  • 打赏
  • 举报
回复
//这里是excel的连接方式.

string str=textBox1.Text ;
str=str.Substring (str.Length -9,5); //取得文件名,因为文件名与内sheet同名,所以可以通过文件名得到sheet1的名.
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+textBox1.Text +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open ();
string strExcel="select * from ["+str+"$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet myDataSet = new DataSet();
try
{

myCommand.Fill(myDataSet,"table1");
}
catch
{
throw;
}
finally
{
conn.Close ();
}




//dataGrid1.DataSource =myDataSet;



//这里是建立access连接串,并准备往里面写东西.
string path1= System.Windows.Forms.Application.StartupPath +"\\data1.mdb";
string strConn1 = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ path1 +";";
OleDbConnection conn1 = new OleDbConnection(strConn1);
string sql1="select * from table1";
OleDbCommand myCommand1=new OleDbCommand(sql1,conn1);
conn1.Open ();



//往数据库中填充数据
foreach(DataRow excelRow in myDataSet.Tables["table1"].Rows)
{
sql1="insert into table1(numb,price,mtotal,date1,cn,cfn,csn,dosa,rout,stren,mea1,vol,mea2,hospcode) values({0},{1},{2},'{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')";
string[] tempArray=new string[11];

//前三个为数据,但仍然要判断.是否为0;

string num1;
string num2;
string num3;



num1=excelRow[0].ToString ();
num2=excelRow[1].ToString ();
num3=excelRow[2].ToString ();

if (num1=="") num1= "0";
if (num2=="") num2="0";
if (num3=="") num3="0";







tempArray[0]=excelRow[3].ToString ();
tempArray[1]=excelRow[4].ToString ();
tempArray[2]=excelRow[5].ToString ();
tempArray[3]=excelRow[6].ToString ();
tempArray[4]=excelRow[7].ToString ();
tempArray[5]=excelRow[8].ToString ();
tempArray[6]=excelRow[9].ToString ();
tempArray[7]=excelRow[10].ToString ();
if (excelRow[11].ToString ()!=null)
tempArray[8]=excelRow[11].ToString ();
else
tempArray[8]=string.Empty ;

if (excelRow[12].ToString ()!=null)
tempArray[9]=excelRow[12].ToString ();
else
tempArray[9]=string.Empty ;

if (excelRow[13].ToString ()!=null)
tempArray[10]=excelRow[13].ToString ();
else
tempArray[10]=string.Empty;






//sql1=string.Format (sql1,excelRow[0],excelRow[1],excelRow[2],tempArray[0],tempArray[1],tempArray[2],tempArray[3],tempArray[4],tempArray[5],tempArray[6],tempArray[7],tempArray[8],tempArray[9],tempArray[10]);
sql1=string.Format (sql1,num1,num2,num3,tempArray[0],tempArray[1],tempArray[2],tempArray[3],tempArray[4],tempArray[5],tempArray[6],tempArray[7],tempArray[8],tempArray[9],tempArray[10]);


myCommand1.CommandText =sql1;
try
{
//myCommand1.CommandText ="insert into table4(numb,price,mtotal,date1,cn,cfn,csn,dosa,rout,stren,mea1,vol,mea2,hospcode) values('11','11','11','11','11','11','11','11','11','11','11','11','11','11')";
myCommand1.ExecuteNonQuery ();
}
catch
{
throw; //just pass the exception up
}
}


这是excel向access数据库中转换的,你把顺序倒转过来便可以了,因为我操作Excel文件,是以把它当数据库来操作的.
gufengsheng 2005-05-16
  • 打赏
  • 举报
回复
http://www.chinacs.net/archives/2/2005/01/28/195.html
我说的是这里面的代码
gufengsheng 2005-05-16
  • 打赏
  • 举报
回复
代码有问题啊
CType(Application.Item("MyDataTable"), DataTable)中的MyDataTable是干什么用的。
gufengsheng 2005-05-15
  • 打赏
  • 举报
回复
有没有简单点的代码就可以实现的呢?
lovinger2000 2005-05-15
  • 打赏
  • 举报
回复
建议你使用ExcelQuicker吧,导出生成Excel报表/文档的控件,性能和稳定性决定很好,可以实现你的需求,在www.eudev.net网站可以下载到最新版本。楼主如果需要实现你需求的代码,可以和我联系,我可以带你完成你的需求,使用C#编写。我的邮箱eudev.net@yeah.net
fankun 2005-05-15
  • 打赏
  • 举报
回复
可以看看vba的文件操作方面的书
yeaky 2005-05-15
  • 打赏
  • 举报
回复
http://www.chinacs.net/archives/2/2005/01/28/195.html
gufengsheng 2005-05-15
  • 打赏
  • 举报
回复
最好用VB.NET代码,C#.NET也可以

62,073

社区成员

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

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

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

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