个位GGJJ,小妹我掉了一地的头发也没有搞定这个问题,现声明一些,我是菜鸟妹妹。刚学C#需求很简单:如下在EXCEL里面有几个竖行,每一列代表data1,data2....data108.
在XML模板里面有data1,data2,data3....data108.
需求:将excel里面的每一个横行的数据替换填写到xml中。生成一个独立的xml文件并且储存到指定的地方。我在不知道的一个地方无意中钞到了一段代码,说得和我要求的一样,但是,不可以用,你说郁闷不,下面那位高手可以帮我看一下哪里有问题啊:using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Datatable ExcelDataSource(string filepath, string sheetname)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter ( "select * from [" + sheetname + "$]", strConn );
DataSet ds = new DataSet ();
oada.Fill ( ds );
return ds.Tables[0] ;//2.将dt中数据行读,遇到需要合并的项,即合并后再写入xml中对应的节点,for (int i = 0; i < dt.Rows.Count; i++)
{
//科目:AB合并
string KM = dt.Rows[i]["A"].ToString() + dt.Rows[i]["B"].ToString();
AddNode(KM);//将新的节点加入目标文件xml中
//其它类似
}
}
//3.将数据存入xml文档中
public bool AddNode(string node)
{
string Fpath = "C:\test.xml";//设置的xml存储路径
XmlDocument xd = new XmlDocument();
xd.Load(Fpath);
XmlElement xe; xe = xd.CreateElement("科目");
xe.InnerText = node; //科目对应的值
}
}
}报以下的错误:
错误 1 “ExcelDataSource”: 不能在静态类中声明实例成员 C:\Documents and Settings\mwzhang\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs 20 26 WindowsFormsApplication3
错误 2 找不到类型或命名空间名称“Datatable”(是否缺少 using 指令或程序集引用?) C:\Documents and Settings\mwzhang\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs 20 16 WindowsFormsApplication3
错误 3 “AddNode”: 不能在静态类中声明实例成员 C:\Documents and Settings\mwzhang\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs 42 21 WindowsFormsApplication3我觉得错误1是没有using datatable的问题但是datatable在哪里啊。。找不到阿。哭
另外,我还有一个问题没有明确,就是在设计的页面,要加入什么的控件呢。其实最好是哪位GG可以帮忙写一个,不用我选择,指定excel和XML模板路径的。这样的话,点一个按钮就可以直接解决了。哈哈做梦了ing....
在XML模板里面有data1,data2,data3....data108.
需求:将excel里面的每一个横行的数据替换填写到xml中。生成一个独立的xml文件并且储存到指定的地方。我在不知道的一个地方无意中钞到了一段代码,说得和我要求的一样,但是,不可以用,你说郁闷不,下面那位高手可以帮我看一下哪里有问题啊:using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Datatable ExcelDataSource(string filepath, string sheetname)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter ( "select * from [" + sheetname + "$]", strConn );
DataSet ds = new DataSet ();
oada.Fill ( ds );
return ds.Tables[0] ;//2.将dt中数据行读,遇到需要合并的项,即合并后再写入xml中对应的节点,for (int i = 0; i < dt.Rows.Count; i++)
{
//科目:AB合并
string KM = dt.Rows[i]["A"].ToString() + dt.Rows[i]["B"].ToString();
AddNode(KM);//将新的节点加入目标文件xml中
//其它类似
}
}
//3.将数据存入xml文档中
public bool AddNode(string node)
{
string Fpath = "C:\test.xml";//设置的xml存储路径
XmlDocument xd = new XmlDocument();
xd.Load(Fpath);
XmlElement xe; xe = xd.CreateElement("科目");
xe.InnerText = node; //科目对应的值
}
}
}报以下的错误:
错误 1 “ExcelDataSource”: 不能在静态类中声明实例成员 C:\Documents and Settings\mwzhang\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs 20 26 WindowsFormsApplication3
错误 2 找不到类型或命名空间名称“Datatable”(是否缺少 using 指令或程序集引用?) C:\Documents and Settings\mwzhang\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs 20 16 WindowsFormsApplication3
错误 3 “AddNode”: 不能在静态类中声明实例成员 C:\Documents and Settings\mwzhang\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs 42 21 WindowsFormsApplication3我觉得错误1是没有using datatable的问题但是datatable在哪里啊。。找不到阿。哭
另外,我还有一个问题没有明确,就是在设计的页面,要加入什么的控件呢。其实最好是哪位GG可以帮忙写一个,不用我选择,指定excel和XML模板路径的。这样的话,点一个按钮就可以直接解决了。哈哈做梦了ing....
抄代码也不是这么抄的.
你把这些代码全都放到Program.cs文件里肯定不对么.
{
ExcelDataSource(excelPath, "Sheet1", xmlPath, "root", "line", "xe");
} public void ExcelDataSource(string excelPath, string sheetName, string xmlPath, string rootName, string lineName, string xeName)
{
string strConn = string.Empty;
if (string.IsNullOrEmpty(sheetName))
{
sheetName = "Sheet1";
}
System.IO.FileInfo file = new System.IO.FileInfo(excelPath);
if (!file.Exists)
{
throw new Exception("文件不存在");
}
string extension = file.Extension;
switch (extension)
{
case ".xls":
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
case ".xlsx":
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
break;
default:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
} System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn);
System.Data.OleDb.OleDbDataAdapter oada = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "$]", strConn);
System.Data.DataSet ds = new System.Data.DataSet();
oada.Fill(ds);
System.Data.DataTable dt = ds.Tables[0];
System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); System.Xml.XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(xmldecl);
System.Xml.XmlElement root = doc.CreateElement(rootName); for (int i = 0; i < dt.Rows.Count; i++)
{
System.Xml.XmlElement line = doc.CreateElement(lineName); for (int j = 0; j < dt.Columns.Count; j++)
{
System.Xml.XmlElement xe = doc.CreateElement(xeName);
xe.InnerText = dt.Rows[i][j].ToString();
line.AppendChild(xe);
} root.AppendChild(line);
} doc.AppendChild(root); doc.Save(xmlPath);
}用的时候直接ExcelDataSource(Excel文件位置,需要保存的xml文件位置);