首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有方法读取.xlsx核心3.1中的.net扩展文件?

有没有方法读取.xlsx核心3.1中的.net扩展文件?
EN

Stack Overflow用户
提问于 2021-05-13 06:07:50
回答 1查看 110关注 0票数 0

我在角10中创建了前端,后端在.net核心3.1中创建

需求:从前端上传文件(.xlsx),在web中进行解析,提取的数据存储在Server中。

使用下面的代码从角上传文件

代码语言:javascript
复制
const formData = new FormData();
    
 for (const file of fileToUpload) {
     formData.append(file.name, file);
 }

我在web中得到了IFormFileCollection格式的文件。

我无法读取web中的文件内容。

(预先谢谢:)

EN

回答 1

Stack Overflow用户

发布于 2022-07-13 13:55:36

代码语言:javascript
复制
      IFormFile postedFile = file[0];
      string fileName = Path.GetFileName(postedFile.FileName);
      string filePath = Path.Combine(Path.GetTempPath(), fileName);
      using (FileStream stream = new FileStream(filePath, FileMode.Create))
      {
           postedFile.CopyTo(stream);
      }
      string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";
      using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
      {
            connection.Open();
            if (workBook != string.Empty)
            {
                 //To apply query on the worksheet we need to apply '$' sign after worksheet name
                 workBook = workBook + "$";
                 //Select all records from worksheet table                        
                 OleDbCommand command = new OleDbCommand("Select * FROM [" + workBook + "]", connection);

                 //Execute the reader
                 using (OleDbDataReader dr = command.ExecuteReader())
                 {
                     try
                     {
                         //Create table
                         sqlConn = new SqlConnection(connString);
                         sqlConn.Open();
                         cmd = new SqlCommand { Connection = sqlConn };

                         // Bulk Copy to created table
                         using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connString))
                         {
                                   bulkCopyData.DestinationTableName = TABLE_NAME;
                                    bulkCopyData.WriteToServer(dr);
                         }
                    }
              }
       }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67514617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档