如何将一个xls文件转换成XML文件

wsxwsx123456789 2009-12-27 10:04:55
如何将一个xls文件转换成XML文件???
...全文
1653 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wsxwsx123456789 2009-12-28
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using Microsoft.Office.Interop.Excel; ----这句为何报错??

警告 1 Imports“Microsoft.Office.Interop.Excel”中指定的命名空间或类型不包含任何公共成员,或者找不到该命名空间或类型。要确保定义了该命名空间或类型且其中至少包含一个公共成员;还要确保导入的元素名不使用任何别名。


wsxwsx123456789 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lcl_data 的回复:]
C# codeusing System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.OleDb;using System.Data.SqlClient;using System.IO;using Microsoft.Office.Interop.Excel;namespace TestAccess
{class Program
{staticvoid Main(string[] args)
{string strConnection="Provider=Microsoft.ACE.OLEDB.12.0;";
strConnection+=@"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;";
strConnection+="Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.Open();
OleDbDataAdapter myCommandd=new OleDbDataAdapter("select * from [Sheet1$]", objConnection);
DataSet ds=new DataSet();
myCommandd.Fill(ds,"[Sheet1$]");
System.Data.DataTable dt= ds.Tables["[Sheet1$]"];
Console.WriteLine(dt.Columns[0].ToString());
Console.WriteLine(dt.Columns[1].ToString());
DataRow drDisplay= dt.Rows[0];int[] num=newint[dt.Columns.Count];for (int j=0; ; )
{for (int i=0; i< dt.Columns.Count; i++)
{if (drDisplay[i]is DBNull) ;else
num[i]+= Convert.ToInt32(drDisplay[i]);

}if (++j>= dt.Rows.Count)break;
drDisplay= dt.Rows[j];
}
objConnection.Close();object MissingValue= Type.Missing;
Microsoft.Office.Interop.Excel.Application app=new Application();
Microsoft.Office.Interop.Excel.Workbook wbook= app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
Microsoft.Office.Interop.Excel.Worksheet wsheet= wbook.ActiveSheetas Microsoft.Office.Interop.Excel.Worksheet;for (int i=0; i< dt.Columns.Count; i++)
{//注意下面是i+1,,excel小标默认从1开始 wsheet.Cells[dt.Rows.Count+2, i+1]= num[i].ToString();
}

wbook.Save();
wbook.Close(true,null,null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
wsheet=null;
wbook=null;
app=null;
GC.Collect();
}
}
}
操作xls文件

[/Quote]



using Microsoft.Office.Interop.Excel;
上面这句,怎么报错,要引用什么??
古今多少事 2009-12-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wsxwsx123456789 的回复:]
感谢楼上,但我需要vb.net 的代码??能否实例化,谢谢
[/Quote]
http://www.developerfusion.com/tools/convert/csharp-to-vb/
wsxwsx123456789 2009-12-27
  • 打赏
  • 举报
回复
感谢楼上,但我需要vb.net 的代码??能否实例化,谢谢
wuyq11 2009-12-27
  • 打赏
  • 举报
回复
DataSet dst=new DataSet();
OleDbConnection sourceConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source ="+strPathName+";Extended Properties=Excel 8.0");
SqlConnection destConn = new SqlConnection(ConnectionString);
string sSql = " SELECT * FROM Sheet1";
OleDbCommand dbCommand = new OleDbCommand();
try
{
sourceConn.Open();
OleDbDataAdapter oleDA = new OleDbDataAdapter(sSql,sourceConn);
oleDA.Fill(dst);
sourceConn.Close();
}
catch
{

}
wuyq11 2009-12-27
  • 打赏
  • 举报
回复
用oledb连接excel将数据读到dataset中
dataset.Writxml
appleller 2009-12-27
  • 打赏
  • 举报
回复
vb.net 顶
十八道胡同 2009-12-27
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace TestAccess
{
class Program
{
static void Main(string[] args)
{

string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConnection += @"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;";
strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection);
DataSet ds = new DataSet();
myCommandd.Fill(ds, "[Sheet1$]");
System.Data.DataTable dt = ds.Tables["[Sheet1$]"];
Console.WriteLine(dt.Columns[0].ToString());
Console.WriteLine(dt.Columns[1].ToString());
DataRow drDisplay = dt.Rows[0];
int[] num = new int[dt.Columns.Count];
for (int j = 0; ; )
{
for (int i = 0; i < dt.Columns.Count; i++)
{

if (drDisplay[i] is DBNull) ;
else
num[i] += Convert.ToInt32(drDisplay[i]);

}
if (++j >= dt.Rows.Count) break;
drDisplay = dt.Rows[j];
}
objConnection.Close();
object MissingValue = Type.Missing;
Microsoft.Office.Interop.Excel.Application app = new Application();
Microsoft.Office.Interop.Excel.Workbook wbook = app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
Microsoft.Office.Interop.Excel.Worksheet wsheet = wbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
for (int i = 0; i < dt.Columns.Count; i++)
{
//注意下面是i+1,,excel小标默认从1开始
wsheet.Cells[dt.Rows.Count + 2, i + 1] = num[i].ToString();
}

wbook.Save();
wbook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
wsheet = null;
wbook = null;
app = null;
GC.Collect();
}
}
}

操作xls文件
十八道胡同 2009-12-27
  • 打赏
  • 举报
回复
我觉的需要读取xls,然后重新生产xml文件
绝代坏坏 2009-12-27
  • 打赏
  • 举报
回复
用sql读进来,通过dataset转成xml即可

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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