C#路径选择及Excel读写操作

C#路径选择及Excel读写操作

最近由于工作需要接触了一下C#对于Excel的操作,来记录一下代码,如有更好的方式希望能够交流一下!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel; 

namespace AutoData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string path = string.Empty;
        public string readstr1 = string.Empty;
        public string readstr2 = string.Empty;
        public string readstr3 = string.E
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#控制台中实现读写Excel,可以使用Microsoft.Office.Interop.Excel库。首先,您需要添加对该库的引用。然后,您可以使用以下代码示例来实现读取和写入Excel文件。 读取Excel文件的示例代码如下: ```csharp using Excel = Microsoft.Office.Interop.Excel; using System.IO; void ReadExcel(string filePath) { Excel.Application app = new Excel.Application(); Excel.Workbook workbook = app.Workbooks.Open(filePath); Excel.Worksheet worksheet = workbook.Sheets\[1\]; int rowCount = worksheet.UsedRange.Rows.Count; int columnCount = worksheet.UsedRange.Columns.Count; for (int i = 1; i <= rowCount; i++) { for (int j = 1; j <= columnCount; j++) { string cellValue = worksheet.Cells\[i, j\].Value.ToString(); Console.Write(cellValue + " "); } Console.WriteLine(); } workbook.Close(); app.Quit(); } string filePath = Path.Combine(System.Environment.CurrentDirectory, "test.xlsx"); ReadExcel(filePath); ``` 写入Excel文件的示例代码如下: ```csharp using Excel = Microsoft.Office.Interop.Excel; using System.IO; void WriteExcel(double\[,\] data, string filePath) { Excel.Application app = new Excel.Application(); Excel.Workbook workbook = app.Workbooks.Add(); Excel.Worksheet worksheet = workbook.Sheets\[1\]; int rowCount = data.GetLength(0); int columnCount = data.GetLength(1); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { worksheet.Cells\[i + 1, j + 1\] = data\[i, j\].ToString(); } } workbook.SaveAs(filePath); workbook.Close(); app.Quit(); } double\[,\] data = new double\[5, 5\]; string filePath = Path.Combine(System.Environment.CurrentDirectory, "test.xlsx"); WriteExcel(data, filePath); Console.WriteLine($"写入到{filePath}"); ``` 请注意,这些示例代码假设您已经安装了Microsoft Office并且在项目中添加了对Interop.Excel库的引用。此外,您还需要根据实际情况更改文件路径和数据。 #### 引用[.reference_title] - *1* *2* *3* [C#读写Excel](https://blog.csdn.net/m0_37816922/article/details/128043199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值