如何读取Excel生成的XML文件?

极速小王子 2004-05-08 06:20:47
如题!!
要求是读出文件中的每个表以及表中的内容,读到一个逻辑空间为“表”的数据结构中
不知道MS提供现成的类没有!
望指点!
...全文
156 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
melonlee 2004-05-09
  • 打赏
  • 举报
回复
string sql1=@"Driver=Microsoft Excel Driver (*.xls);DBQ=d:\temp\"+this.Session["username"].ToString()+".xls";
string sql2="SELECT distinct * FROM [Sheet1$] ";
OdbcConnection myConnection = new OdbcConnection(sql);
OdbcDataAdapter myCommand=null;
try
{
myCommand = new OdbcDataAdapter(sql2, myConnection);
}
catch
{
this.Label5.Text="打开excel表失败";
return;
}
DataSet ds = new DataSet();
myCommand.Fill(ds, "newtable");
ds.writexml
.
.
.
极速小王子 2004-05-09
  • 打赏
  • 举报
回复
注意,我的问题是不通过SQL,而是直接通过文件操作或MS提供的类来读取,有现成的么?
fqcd555 2004-05-09
  • 打赏
  • 举报
回复
顶。。
tommyhuanglei 2004-05-08
  • 打赏
  • 举报
回复
从excel里导入到dataset里,然后dataset.writexml
jizhe 2004-05-08
  • 打赏
  • 举报
回复
string connectionString = "Driver={Microsoft Excel Driver (*.xls)};DBQ=" + filename;

ODBCAccess odbc = new ODBCAccess(connectionString);
string sql = "select * from [" + 每个表+ "$]";

运行sql取得DataSet
再用下面函数写入文件

private void WriteXmlToFile(DataSet thisDataSet) {
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "myXmlDoc.xml";
// Create the FileStream to write with.
System.IO.FileStream myFileStream = new System.IO.FileStream
(filename, System.IO.FileMode.Create);
// Create an XmlTextWriter with the fileStream.
System.Xml.XmlTextWriter myXmlWriter =
new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(myXmlWriter);
myXmlWriter.Close();
}
oceanju 2004-05-08
  • 打赏
  • 举报
回复
没有,你只能用Excel.Application对象来读出excel的内容,然后自己写入表中

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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