0

尝试使用softartisans ExcelWriter从excel工作表导入批量数据。我没有找到很多示例代码来逐行读取并插入数据库我可以获取示例代码以在c#中逐行读取和插入

导入的excel文件保存在临时位置并开始读取记录

尝试

        {

            string temp_file_name = StateManager.NetworkID + DateTime.Now.Ticks.ToString() + ".xls";

            this.import_file_upload_box.PostedFile.SaveAs(Server.MapPath("temp_files") + "\\" + temp_file_name);

            string strConn;

            recct = ThisWorkbook.Sheets("Project_Details").Range("A2", ThisWorkbook.Sheets("Project_Details").Range("A2").End(xlDown)).Rows.Count;



            ExcelApplication xla = new ExcelApplication();

            Workbook wb = xla.Create(ExcelApplication.FileFormat.Xlsx);

            Worksheet ws = wb.Worksheets[0];

        }

        catch

        {



        }

实际结果应在导入的 Excel 上逐行读取

4

2 回答 2

0

你为什么不像下面的例子那样做一个大容量插入呢?

下面的代码可以在这个链接上找到

INSERT INTO dbo.ImportTest 

SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', 

'Data Source=C:\ImportData.xls;Extended Properties=Excel 8.0')...[Sheet1$]
于 2019-08-06T12:41:58.647 回答
0

我通常为此使用OLEDB。
- 将其导入数据表
- 进行验证
- 写入数据库

public static DataTable LoadCSV(string fileName)
        {
            string sqlString = "Select * FROM [" + fileName + "];";
            string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                + InboundSourceFilePath + ";" + "Extended Properties='text;HDR=NO;'";
            DataTable theCSV = new DataTable();

            using (OleDbConnection conn = new OleDbConnection(conStr))
            {
                using (OleDbCommand comm = new OleDbCommand(sqlString, conn))
                {
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(comm))
                    {
                        adapter.Fill(theCSV);
                    }
                }
            }
            return theCSV;
        }
于 2019-08-06T13:21:04.670 回答