C#路径选择及Excel读写操作
来源:互联网 发布:知乎,京九高铁 编辑:程序博客网 时间:2024/06/05 23:47
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.Empty; public string fileName = string.Empty; private string read_excel(int readsheet,int x,int y) { IWorkbook workbook = null; //新建IWorkbook对象 fileName = @path + "/123.xlsx"; FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 { workbook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook } else if (fileName.IndexOf(".xls") > 0) // 2003版本 { workbook = new HSSFWorkbook(fileStream); //xls数据读入workbook } fileStream.Close(); //关闭文件流 ISheet sheet = workbook.GetSheetAt(readsheet-1); //获取第一个工作表 workbook.Close(); IRow row; row = sheet.GetRow(x-1); //int rowcount = sheet.LastRowNum; string cellValue = row.GetCell(y-1).ToString(); Console.WriteLine(cellValue); return cellValue; } private bool write_excle(int readsheet,int x,int y,string value) { //IWorkbook workbook = null; //新建IWorkbook对象 // fileName = @path + "/123.xlsx"; //FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //if (fileName.IndexOf(".xlsx") > 0) // 2007版本 //{ // workbook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook //} //else if (fileName.IndexOf(".xls") > 0) // 2003版本 //{ // workbook = new HSSFWorkbook(fileStream); //xls数据读入workbook //} //ISheet sheet = workbook.GetSheetAt(readsheet - 1); //获取第一个工作表 //ICell cell = sheet.GetRow(x - 1).GetCell(y - 1); //cell.SetCellValue(value); //workbook.Write(fileStream); //Console.WriteLine("写入成功!"); //fileStream.Close(); //关闭文件流 //workbook.Close(); IWorkbook workbook = new XSSFWorkbook(); workbook.CreateSheet("sheet1");//创建sheet fileName = @path + "/123.xlsx"; FileStream fs = File.Create(fileName);//path=mmm.xls; ISheet sheet = workbook.GetSheetAt(readsheet-1);//获取sheet sheet.CreateRow(5).CreateCell(4).SetCellValue(value); workbook.Write(fs); return true; } private void button1_Click(object sender, EventArgs e) { System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { path = fbd.SelectedPath; } textBox1.Text = path; } private void button2_Click(object sender, EventArgs e) { bool ret = false; readstr1 = read_excel(1,3,3); // textBox1.Text = readstr1; ret = write_excle(1, 4, 3, readstr1); if (ret) { textBox1.Text = "OK"; } } }}
阅读全文
0 0
- C#路径选择及Excel读写操作
- C# 读写操作Excel
- C# Excel操作读写实例
- c# winform 路径选择和文件读写
- C#对EXCEL的读写操作
- C#导出到Excel,读写操作
- C#对EXCEL的读写操作
- C#导出到Excel,读写操作
- C#对Excel的读写操作
- C# Excel ole文件读写操作类
- C#读写EXCEL操作的简单封装
- c#导出Excel及操作
- c#操作excel方式一:stream简单读写excel
- c#操作excel方式一:stream简单读写excel
- C#读写操作Excel文件,Excel进程资源回收代码
- C#文件读写及相关操作
- C#文件读写及相关操作
- [转]C#文件读写及相关操作
- 如何查看USB设备的VID\PID
- EasyAR+Unity开发(二)HelloAR 项目剖析以及具体使用
- 单双回文(简单来说就是把一个回文字符串劈成两个)
- 云计算笔记二:Hadoop概述
- javascript对象
- C#路径选择及Excel读写操作
- Python-matplotlib入门--基础图表的绘制
- 抽象类(abstract class)和接口(interface)有什么异同?
- 按键驱动程序
- Java开发工程师的面试该用什么姿势?做到这些让你多拿5K
- 基于Unity3D的相机功能的实现(四)——第三人称相机(TPS)
- Choosing Capital for Treeland (树形dp+双向搜索)
- Caffe编译Bug
- java 设计模式:构建器模式