c# 操作excel文件(csv、数据库)
2011-10-10 11:06
225 查看
1、把csv文件中的数据读到datatable中
public static DataTable ImportCSV(string filePath)
{
DataTable ds = new DataTable();
using (StreamReader sw = new StreamReader(filePath,Encoding.Default))
{
string str = sw.ReadLine();
string[] columns = str.Split(',');
foreach (string name in columns)
{
ds.Columns.Add(name);
}
string line = sw.ReadLine();
while (line != null)
{
string[] data = line.Split(',');
ds.Rows.Add(data);
line = sw.ReadLine();
}
sw.Close();
}
return ds;
}
2、把csv的数据导入到数据库中
public static void ImportCSVToDB(string filePath, string type, string dbTable)
{
string sqlString = "BULT insert " + dbTable + " from '" + filePath + "' WITH(FILEDTERMINATOR=',',ROWTERMINATOR='\n')";
using (SqlConnection conn = GetConn(conStr))
{
conn.Open();
SqlCommand command = new SqlCommand(sqlString, conn);
return command.ExecuteNonQuery();
}
}
3、excel的数据读入datatable中
public static DataTable ImportXLS(string filePath)
{
string fileName = System.IO.Path.GetFileName(filePath);
string path = System.IO.Path.GetDirectoryName(filePath);
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties = 'Excel 8.0;HDR=YES;IMEX=1;'";
DataTable ds = new DataTable();
using (OleDbConnection conn = new OleDbConnection(strCon))
{
conn.Open();
OleDbDataAdapter adpt = new OleDbDataAdapter("select * from [Sheet1$]", conn);
adpt.Fill(ds);
}
return ds;
}
4、导出数据到csv文件中。(这种方式速度快)
public static void ExportToCSV(DataTable ds, string filePath)
{
using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.Unicode))
{
for (int i = 0; i < ds.Columns.Count; i++)
{
sw.Write(ds.Columns[i].ColumnName + "\t");
}
sw.Write("\n");
for (int i = 0; i < ds.Rows.Count; i++)
{
for (int j = 0; j < ds.Columns.Count; j++)
{
sw.Write(ds.Rows[i][j].ToString() + "\t");
}
sw.Write("\n");
}
sw.Close();
}
return ;
}
public static DataTable ImportCSV(string filePath)
{
DataTable ds = new DataTable();
using (StreamReader sw = new StreamReader(filePath,Encoding.Default))
{
string str = sw.ReadLine();
string[] columns = str.Split(',');
foreach (string name in columns)
{
ds.Columns.Add(name);
}
string line = sw.ReadLine();
while (line != null)
{
string[] data = line.Split(',');
ds.Rows.Add(data);
line = sw.ReadLine();
}
sw.Close();
}
return ds;
}
2、把csv的数据导入到数据库中
public static void ImportCSVToDB(string filePath, string type, string dbTable)
{
string sqlString = "BULT insert " + dbTable + " from '" + filePath + "' WITH(FILEDTERMINATOR=',',ROWTERMINATOR='\n')";
using (SqlConnection conn = GetConn(conStr))
{
conn.Open();
SqlCommand command = new SqlCommand(sqlString, conn);
return command.ExecuteNonQuery();
}
}
3、excel的数据读入datatable中
public static DataTable ImportXLS(string filePath)
{
string fileName = System.IO.Path.GetFileName(filePath);
string path = System.IO.Path.GetDirectoryName(filePath);
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties = 'Excel 8.0;HDR=YES;IMEX=1;'";
DataTable ds = new DataTable();
using (OleDbConnection conn = new OleDbConnection(strCon))
{
conn.Open();
OleDbDataAdapter adpt = new OleDbDataAdapter("select * from [Sheet1$]", conn);
adpt.Fill(ds);
}
return ds;
}
4、导出数据到csv文件中。(这种方式速度快)
public static void ExportToCSV(DataTable ds, string filePath)
{
using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.Unicode))
{
for (int i = 0; i < ds.Columns.Count; i++)
{
sw.Write(ds.Columns[i].ColumnName + "\t");
}
sw.Write("\n");
for (int i = 0; i < ds.Rows.Count; i++)
{
for (int j = 0; j < ds.Columns.Count; j++)
{
sw.Write(ds.Rows[i][j].ToString() + "\t");
}
sw.Write("\n");
}
sw.Close();
}
return ;
}
相关文章推荐
- C#以操作数据库方式访问EXCEL文件的几个常见问题及解决方法
- c# 命令行操作数据库并将数据导出到csv
- C#操作Excel文件
- C# .Net :Excel NPOI导入导出操作教程之将Excel文件读取并写到数据库表,示例分享
- C#操作Excel文件(读取Excel,写入Excel) .
- C#对数据库的操作
- C#控件TreeView与数据库的操作
- C# 操作 XML 数据库类型、Oracle XMLType
- c# 操作数据库
- C#读写操作Excel文件,Excel进程资源回收代码
- 【转】C# 连接 MySQL 并进行数据库操作(入门篇)
- C# 中的数据库操作~存储过程篇Mysql SqlServer
- sqllite (2) - c# .net 使用 sqllite 增删改查操作数据库
- C#中对数据库的基本操作(增删改以及调用存储过程)
- C#数据库操作(阅读器、数据库绑定、修改、统计、分页)
- c#对数据库的操作代码(MySql.Data.dll包)
- 操作 SQL Server Mobile 2005 数据库的常用 C# 代码 (转自黎波)
- C#编写聊天程序系列笔记(三)数据库加锁操作
- C# 数据库操作基类
- [原创]C#通过引用Office Excel (2007) 组件实现对Excel文件的操作