使用ExcelDataReader v2.1从C#中的XLSX读取数据并写入SQL Server
我需要用C语言编写一个控制台应用程序,从XLSX读取数据,并使用ExcelDataReader v2.1写入SQL server 我需要读取单元格D17、D18、D19中的数据,然后转到SQL中的购买列。 然后读取单元格K17、K18、K19,并将其放到SQL中的/selling列 这是SQL表查询,数据库名称为test1使用ExcelDataReader v2.1从C#中的XLSX读取数据并写入SQL Server,c#,sql,excel,C#,Sql,Excel,我需要用C语言编写一个控制台应用程序,从XLSX读取数据,并使用ExcelDataReader v2.1写入SQL server 我需要读取单元格D17、D18、D19中的数据,然后转到SQL中的购买列。 然后读取单元格K17、K18、K19,并将其放到SQL中的/selling列 这是SQL表查询,数据库名称为test1 SELECT TOP 1000 [id] ,[code] ,[buying] ,[selling] FROM [test1].[dbo
SELECT TOP 1000 [id]
,[code]
,[buying]
,[selling]
FROM [test1].[dbo].[Currency]
这是我尝试过的代码,但它没有使用ExcelDataReader v2.1。我需要帮助才能使用ExcelDataReader v2.1完成此操作
using System;
using System.IO;
using Bytescout.Spreadsheet;
using System.Data.SqlClient;
namespace ExportToSQLServer
{
class Program
{
static void Main(string[] args)
{
try
{
string connectionString = "Data Source=192.168.1.215;Initial Catalog=RECIPES2;Persist Security Info=True;User ID=sa;Password=***********";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
ExecuteQueryWithoutResult(connection, "USE test1");
//Load XLS document
using (Spreadsheet document = new Spreadsheet())
{
document.LoadFromFile("SimpleReport.xls");
Worksheet worksheet = document.Workbook.Worksheets[0];
for (int row = 0; row <= worksheet.UsedRangeRowMax; row++)
{
String insertCommand = string.Format("INSERT XlsTest VALUES('{0}','{1}')",
worksheet.Cell(row, 0).Value, worksheet.Cell(row, 1).Value);
ExecuteQueryWithoutResult(connection, insertCommand);
}
}
// Check the data successfully exported
using (SqlCommand command = new SqlCommand("SELECT * from XlsTest", connection))
{
SqlDataReader reader = command.ExecuteReader();
if (reader != null)
{
Console.WriteLine();
Console.WriteLine("Exported XLS data:");
Console.WriteLine();
while (reader.Read())
{
Console.WriteLine(String.Format("{0} | {1}", reader[0], reader[1]));
}
}
}
Console.WriteLine();
Console.WriteLine("Press any key.");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.ReadKey();
}
}
static void ExecuteQueryWithoutResult(SqlConnection connection, string query)
{
using (SqlCommand command = new SqlCommand(query, connection))
{
command.ExecuteNonQuery();
}
}
}
}
使用系统;
使用System.IO;
使用Bytescout.电子表格;
使用System.Data.SqlClient;
命名空间ExportToSQLServer
{
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
string connectionString=“数据源=192.168.1.215;初始目录=RECIPES2;持久安全信息=True;用户ID=sa;密码=********”;
使用(SqlConnection连接=新的SqlConnection(connectionString))
{
connection.Open();
ExecuteQueryWithoutResult(连接,“使用test1”);
//加载XLS文档
使用(电子表格文档=新电子表格())
{
document.LoadFromFile(“SimpleReport.xls”);
工作表=文档.工作簿.工作表[0];
对于(int row=0;row)