EXCEL导入
try
{
OpenFileDialog of = new OpenFileDialog();
of.Filter = "(*.xlsx;*.xls)|*.xlsx;*.xls";
of.Title = "请选择需要导入的.xlsx文件,并确保该文件是打开状态";
string filename;
if (of.ShowDialog() == DialogResult.OK)
{
filename = of.FileName;
string con = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended
Properties=Excel 5.0;";
OleDbConnection conn = new OleDbConnection(con);
string sql = String.Format("select * from [{0}$]", tBsheetname.Text.Trim());
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "全部");
if (MessageBox.Show("导入项目不会覆盖之前导入过的内容,本次将导入" + ds.Tables[0].Rows.Count +
"个床位信息,是否仍要继续?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
== DialogResult.Yes)
{
try
{
progressBar1.Visible = true;
progressBar1.Minimum = 0;
progressBar1.Maximum = ds.Tables[0].Rows.Count;
progressBar1.Value = progressBar1.Minimum;
//DB.sqlEx("truncate table " + sTable);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
OleDbDataReader dr = DB.reDr("select * from 嵩县_医保床位 where 医保编码='" +
ds.Tables[0].Rows[i]["床位号"].ToString().Trim() + "'");
if (!dr.HasRows)
{
string insert = "insert into 嵩县_医保床位(医保编码,医保名称) values ('";
insert += ds.Tables[0].Rows[i]["床位号"].ToString().Trim() + "','";
insert += ds.Tables[0].Rows[i]["床位名称"].ToString().Trim() + "')";
DB.sqlEx(insert);
}
DB.sqlEx("update 嵩县_医保床位 set " +
" 床位类型='" + ds.Tables[0].Rows[i]["床位类型"].ToString().Trim() + "'," +
" 中心标准='" + ds.Tables[0].Rows[i]["中心标准"].ToString().Trim() + "'," +
"医保名称='" + ds.Tables[0].Rows[i]["床位名称"].ToString().Trim() + "' where
医保编码='" + ds.Tables[0].Rows[i]["床位号"].ToString().Trim() + "'");
progressBar1.Value++;
}
MessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
progressBar1.Visible = false;
Query();
}
catch (Exception ex)
{
MessageBox.Show("请选择正确的文件!\r\n" + ex.ToString(), "警告", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
注: ds.Tables[0]的列名是excel中的第一行。
CONFIG.APP
public static string ConnectionString
{
get
{
String strCon = "";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(System.AppDomain.CurrentDomain.BaseDirectory + "app.config");
XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("add");
for (int i = 0; i < xmlNodes.Count; i++)
{
XmlAttribute xmlAttr = xmlNodes[i].Attributes["key"];
if (xmlAttr.Value == "ConnectionString")
{
xmlAttr = xmlNodes[i].Attributes["value"];
strCon = xmlAttr.Value;
break;
}
}
return strCon;
}
}
INI
C#并不像C++,拥有属于自己的类库。C#使用的类库是.Net框架为所有.Net程序开发提供的一个共有的类库——.Net
FrameWork SDK。虽然.Net FrameWork SDK内容十分庞大,功能也非常强大,但还不能面面俱到,至少它并没有提供直接操作INI文
件所需要的相关的类。在本文中,C#操作INI文件使用的是Windows系统自带Win32的API函数——WritePrivateProfileString()
和GetPrivateProfileString()函数。这二个函数都位于“kernel32.dll”文件中。
下例,输入section 和key ,就可以取里面的值了。
private void button3_Click ( object sender , System.EventArgs e )
{
StringBuilder temp = new StringBuilder ( 255 ) ;
string FileName = textBox1.Text ;
string section = textBox2.Text;
string key = textBox3.Text ;
int i = GetPrivateProfileString ( section , key , "无法读取对应数值!" , temp , 255 , FileName ) ;
//显示读取的数值
textBox4.Text = temp.ToString();
}
//写入INI文件
private void button2_Click ( object sender , System.EventArgs e )
{
string FileName = textBox1.Text
string section = textBox2.Text
string key = textBox3.Text
string keyValue = textBox4.Text
WritePrivateProfileString ( section , key , keyValue , FileName )
MessageBox.Show( "成功写入INI文件!" , "信息" )
}
OLEDB
oledb既可以访问数据库,也可以访问excel,与ado本质上是一种技术。ado.net与oledb比较而言,ado.net访问速度更快但只使用
sqlserver,oledb更加通用但速度不及ado.net。
我们用DBhelper.dll来控制oledb,操作简便。有需要DBhelper.dll的可以给我留言。
OleDbDataReader read = DB.reDr("Select * from 表1");
while (read.Read())
{
string xx = read["编码"].ToString();
}
read.Close();
XML
有两种方式:
方法一:
XmlDocument doc = new XmlDocument();
2 doc.Load("config.xml"); //加载Xml文件
3 XmlElement rootElem = doc.DocumentElement; //获取根节点
4 XmlNodeList personNodes = rootElem.GetElementsByTagName("person"); //获取person子节点集合
5 foreach (XmlNode node in personNodes)
6 {
7 string strName = ((XmlElement)node).GetAttribute("name"); //获取name属性值
8 Console.WriteLine(strName);
9 XmlNodeList subAgeNodes = ((XmlElement)node).GetElementsByTagName("age"); //获取age子XmlElement集合
10 if (subAgeNodes.Count == 1)
11 {
12 string strAge = subAgeNodes[0].InnerText;
13 Console.WriteLine(strAge);
14 }
15 }
方法二:
try
{
// 打开一个 XML 文件
XmlTextReader reader = new XmlTextReader("C:\\temp\\RCMSInterface.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
MessageBox.Show(reader.Name);
break;
case XmlNodeType.Text:
MessageBox.Show(reader.Value);
break;
case XmlNodeType.CDATA:
MessageBox.Show(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
MessageBox.Show(reader.Name+reader.Value);
break;
case XmlNodeType.Comment:
MessageBox.Show(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
MessageBox.Show("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
MessageBox.Show(reader.Name + reader.Value);
break;
case XmlNodeType.EntityReference:
MessageBox.Show(reader.Name);
break;
case XmlNodeType.EndElement:
MessageBox.Show(reader.Name);
break;
}
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
try
{
OpenFileDialog of = new OpenFileDialog();
of.Filter = "(*.xlsx;*.xls)|*.xlsx;*.xls";
of.Title = "请选择需要导入的.xlsx文件,并确保该文件是打开状态";
string filename;
if (of.ShowDialog() == DialogResult.OK)
{
filename = of.FileName;
string con = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended
Properties=Excel 5.0;";
OleDbConnection conn = new OleDbConnection(con);
string sql = String.Format("select * from [{0}$]", tBsheetname.Text.Trim());
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "全部");
if (MessageBox.Show("导入项目不会覆盖之前导入过的内容,本次将导入" + ds.Tables[0].Rows.Count +
"个床位信息,是否仍要继续?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
== DialogResult.Yes)
{
try
{
progressBar1.Visible = true;
progressBar1.Minimum = 0;
progressBar1.Maximum = ds.Tables[0].Rows.Count;
progressBar1.Value = progressBar1.Minimum;
//DB.sqlEx("truncate table " + sTable);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
OleDbDataReader dr = DB.reDr("select * from 嵩县_医保床位 where 医保编码='" +
ds.Tables[0].Rows[i]["床位号"].ToString().Trim() + "'");
if (!dr.HasRows)
{
string insert = "insert into 嵩县_医保床位(医保编码,医保名称) values ('";
insert += ds.Tables[0].Rows[i]["床位号"].ToString().Trim() + "','";
insert += ds.Tables[0].Rows[i]["床位名称"].ToString().Trim() + "')";
DB.sqlEx(insert);
}
DB.sqlEx("update 嵩县_医保床位 set " +
" 床位类型='" + ds.Tables[0].Rows[i]["床位类型"].ToString().Trim() + "'," +
" 中心标准='" + ds.Tables[0].Rows[i]["中心标准"].ToString().Trim() + "'," +
"医保名称='" + ds.Tables[0].Rows[i]["床位名称"].ToString().Trim() + "' where
医保编码='" + ds.Tables[0].Rows[i]["床位号"].ToString().Trim() + "'");
progressBar1.Value++;
}
MessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Information);
progressBar1.Visible = false;
Query();
}
catch (Exception ex)
{
MessageBox.Show("请选择正确的文件!\r\n" + ex.ToString(), "警告", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
注: ds.Tables[0]的列名是excel中的第一行。
CONFIG.APP
public static string ConnectionString
{
get
{
String strCon = "";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(System.AppDomain.CurrentDomain.BaseDirectory + "app.config");
XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("add");
for (int i = 0; i < xmlNodes.Count; i++)
{
XmlAttribute xmlAttr = xmlNodes[i].Attributes["key"];
if (xmlAttr.Value == "ConnectionString")
{
xmlAttr = xmlNodes[i].Attributes["value"];
strCon = xmlAttr.Value;
break;
}
}
return strCon;
}
}
INI
C#并不像C++,拥有属于自己的类库。C#使用的类库是.Net框架为所有.Net程序开发提供的一个共有的类库——.Net
FrameWork SDK。虽然.Net FrameWork SDK内容十分庞大,功能也非常强大,但还不能面面俱到,至少它并没有提供直接操作INI文
件所需要的相关的类。在本文中,C#操作INI文件使用的是Windows系统自带Win32的API函数——WritePrivateProfileString()
和GetPrivateProfileString()函数。这二个函数都位于“kernel32.dll”文件中。
下例,输入section 和key ,就可以取里面的值了。
private void button3_Click ( object sender , System.EventArgs e )
{
StringBuilder temp = new StringBuilder ( 255 ) ;
string FileName = textBox1.Text ;
string section = textBox2.Text;
string key = textBox3.Text ;
int i = GetPrivateProfileString ( section , key , "无法读取对应数值!" , temp , 255 , FileName ) ;
//显示读取的数值
textBox4.Text = temp.ToString();
}
//写入INI文件
private void button2_Click ( object sender , System.EventArgs e )
{
string FileName = textBox1.Text
string section = textBox2.Text
string key = textBox3.Text
string keyValue = textBox4.Text
WritePrivateProfileString ( section , key , keyValue , FileName )
MessageBox.Show( "成功写入INI文件!" , "信息" )
}
OLEDB
oledb既可以访问数据库,也可以访问excel,与ado本质上是一种技术。ado.net与oledb比较而言,ado.net访问速度更快但只使用
sqlserver,oledb更加通用但速度不及ado.net。
我们用DBhelper.dll来控制oledb,操作简便。有需要DBhelper.dll的可以给我留言。
OleDbDataReader read = DB.reDr("Select * from 表1");
while (read.Read())
{
string xx = read["编码"].ToString();
}
read.Close();
XML
有两种方式:
方法一:
XmlDocument doc = new XmlDocument();
2 doc.Load("config.xml"); //加载Xml文件
3 XmlElement rootElem = doc.DocumentElement; //获取根节点
4 XmlNodeList personNodes = rootElem.GetElementsByTagName("person"); //获取person子节点集合
5 foreach (XmlNode node in personNodes)
6 {
7 string strName = ((XmlElement)node).GetAttribute("name"); //获取name属性值
8 Console.WriteLine(strName);
9 XmlNodeList subAgeNodes = ((XmlElement)node).GetElementsByTagName("age"); //获取age子XmlElement集合
10 if (subAgeNodes.Count == 1)
11 {
12 string strAge = subAgeNodes[0].InnerText;
13 Console.WriteLine(strAge);
14 }
15 }
方法二:
try
{
// 打开一个 XML 文件
XmlTextReader reader = new XmlTextReader("C:\\temp\\RCMSInterface.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
MessageBox.Show(reader.Name);
break;
case XmlNodeType.Text:
MessageBox.Show(reader.Value);
break;
case XmlNodeType.CDATA:
MessageBox.Show(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
MessageBox.Show(reader.Name+reader.Value);
break;
case XmlNodeType.Comment:
MessageBox.Show(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
MessageBox.Show("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
MessageBox.Show(reader.Name + reader.Value);
break;
case XmlNodeType.EntityReference:
MessageBox.Show(reader.Name);
break;
case XmlNodeType.EndElement:
MessageBox.Show(reader.Name);
break;
}
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}