C# Excel 读写数据

VS-C++ 系列:所有相关C++文章链接.
VS-C# 系列:所有相关C#文章链接.
bat 系列:所有相关bat文章链接.


Keil 系列:所有相关文章链接
所有内容均以最小系统调试成功;逐步提供低分源码工程下载
保证每行代码都经过验证!
如有疑惑,欢迎留言,看见即回;祝好__by Dxg_LC

序言:
1、以上链接为方便整理查看资料用;伴随博文发布更新,如果有不正确处,感谢指正
2、因本人能力有限若有不正确之处或者相关超链接失效,请于相关文章内提醒@博主;灰常感谢
3、友情提醒1,勿要《一支烟 + 一杯茶 == 一坐一下午》 身体重要,革命本钱;
4、友情提醒2,多喝热水;
5、友情提醒3,听媳妇话+多点时间陪家人;
在这里插入图片描述

1、C# 读Excel数据

OpenFileDialog fdlg = new OpenFileDialog();
//fdlg.Filter = "*.jpg|*.jpg;*.JPEG;*.JPG;*.jpeg|*.avi|*.avi;";
fdlg.Filter = "所有文件(*.*) | *.*";
fdlg.Multiselect = false;
if (fdlg.ShowDialog() == DialogResult.OK)
{
	if (System.IO.Path.GetExtension(fdlg.FileName) == ".xlsx" || System.IO.Path.GetExtension(fdlg.FileName) == ".xls")
	{
		;
	}
	else
	{
		return;
	}

	MSExcel.Application excelApp;            //Excel应用程序变量
	MSExcel.Workbook excelDoc;               //Excel文档变量
	excelApp = new MSExcel.Application();    //初始化

	p_str_ExcelPutIn = fdlg.FileName;
	excelApp.DisplayAlerts = false;
	excelApp.Visible = false;
	excelApp.ScreenUpdating = false;

	//如果已存在,则打开
	if (File.Exists((string)p_str_ExcelPutIn))
	{
		excelDoc = excelApp.Workbooks.Open(p_str_ExcelPutIn, System.Type.Missing, System.Type.Missing, System.Type.Missing,
				   System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
				   System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
	}
	else
	{
		return;
	}

	//由于使用的是COM库,因此有许多变量需要用Nothing代替
	Object Nothing = Missing.Value;

	//使用第一个工作表作为插入数据的工作表
	MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheets[1];

	//声明一个MSExcel.Range 类型的变量r
	MSExcel.Range r;
	//获得A1处的表格,并赋值
	int _CountW = ws.Columns.Count;//最大的W,不是有有效值
	int _CountH = ws.Rows.Count;//最大的H,不是有有效值

	for (int i = 1; i < 10; i++)
	{
	 r = ws.get_Range("A" + (i).ToString(), "A" + (i).ToString());//A*
	 Console.WriteLine("A" + (i).ToString() + " " + r.value2.ToString());
	 
	 r = ws.get_Range("B" + (i).ToString(), "B" + (i).ToString());//B*
	 Console.WriteLine("B" + (i).ToString() + " " + r.value2.ToString());
	}

	//关闭excelDoc文档对象
	excelDoc.Close(Nothing, Nothing, Nothing);
	//关闭excelApp组件对象
	excelApp.Quit();
	Console.WriteLine(p_str_ExcelPutOut + " 创建完毕!");
}

1、C# 写Excel数据

p_str_ExcelPutOut = System.Environment.CurrentDirectory + "\\语言翻译文件" + DateTime.Now.ToString("HHMMss") + ".xls";
if (p_str_xmlPutIn == "")
    return;

MSExcel.Application excelApp;            //Excel应用程序变量
MSExcel.Workbook excelDoc;               //Excel文档变量

excelApp = new MSExcel.Application();    //初始化
excelApp.DisplayAlerts = false;
excelApp.Visible = false;
excelApp.ScreenUpdating = false;

//如果已存在,则删除
if (File.Exists((string)p_str_ExcelPutOut))
{
    File.Delete((string)p_str_ExcelPutOut);
}
//由于使用的是COM库,因此有许多变量需要用Nothing代替
Object Nothing = Missing.Value;
excelDoc = excelApp.Workbooks.Add(Nothing);

//使用第一个工作表作为插入数据的工作表
MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheets[1];
//声明一个MSExcel.Range 类型的变量r
MSExcel.Range r;
//获得A1处的表格,并赋值
r = ws.get_Range("A1", "A1");
r.Value2 = "key";
//获得A2处的表格,并赋值
r = ws.get_Range("B1", "B1");
r.Value2 = "Chinese";
//获得A3处的表格,并赋值
r = ws.get_Range("C1", "C1");
r.Value2 = "English";

for (int i = 1; i < 10; i++)
{
    //Key
    r = ws.get_Range("A" + (i + 1).ToString(), "A" + (i + 1).ToString());
    r.Value2 = "A" + i.ToString();

    //Chinese
    r = ws.get_Range("B" + (i + 1).ToString(), "B" + (i + 1).ToString());
    r.Value2 = "B" + i.ToString();
}

//WdSaveFormat为Excel文档的保存格式
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
//将excelDoc文档对象的内容保存为XLSX文档
excelDoc.SaveAs(p_str_ExcelPutOut, format, Nothing, Nothing, Nothing, Nothing, MSExcel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭excelDoc文档对象
excelDoc.Close(Nothing, Nothing, Nothing);
//关闭excelApp组件对象
excelApp.Quit();
Console.WriteLine(p_str_ExcelPutOut + " 创建完毕!");

其他的方法参考
1、https://github.com/nissl-lab/npoi

Dxg-原创出品,如需转载,请注明出处;

欢迎收藏,点赞;"一键三联"走起,LOL

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淘气坏坏besos

原创干货分析,欢迎大佬打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值