c# excel工作表及cell读取 .
转发至:http://blog.csdn.net/chuangxin/article/details/6085577
昨天晚上做了一回雷锋,帮朋友写了个程序,把他2天都未整理好的Excel数据,一个小时搞定。大致如下:我朋友是搞质检的,每天都会产生一个excel文件,每个excel文件都有20来个工作表,他要做的事情是把每个文件的每个工作表的其中6个单元格数据提取出来产生excel台账。悲哀的是,他要处理将近12个月的数据,累计300多个excel文件,估计他已经抓狂了。无奈,我用C#给他写了个excel工作表及cell读取的程序,因为只是他用,读取工作表的那些cell都写死了,没写成可配置。
处理如下:
1)文件夹excel文件遍历
每个月的excel文件都存放在一个目录中,如201001,因此,我要读取该目录,并遍历出该目录的所有excel文件,当然读取哪个目录,他自己指定。
2)excel工作表读取
由于每个excel文件都有10到20个工作表,因此需要遍历读取没个工作表的指定cell信息。
3)excel 工作表单元格读取
4)读取到的单元格信息汇总成行输出
简单起见,每个工作表的所有cell值输出成一行,并以/t分隔开,每行结尾输出换行符/r/n,输出到文本框控件。这样我朋友只要复制文本框中的所有信息到excel即可。
5)当然为了告知当前已经处理到指定目录的哪个excel文件,添加了一个进度条
界面如下:
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using Excel; using System.Reflection; namespace jiangbt { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //文件夹设置 private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog(); if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { txtFolderPath.Text = folderBrowserDialog1.SelectedPath; retrieveFolder(); } folderBrowserDialog1.Dispose(); } private void retrieveFolder() { string[] filename = Directory.GetFiles(txtFolderPath.Text); listFiles.Items.Clear(); if (filename != null && filename.Length > 0) { for (int i = 0; i < filename.Length; i++) { if (filename[i].LastIndexOf(".") > 0) { String filetype = filename[i].Substring(filename[i].LastIndexOf(".") + 1); filetype = filetype.ToLower(); if (filetype.Equals("xls")) { listFiles.Items.Add(filename[i]); } } } progressBar1.Minimum = 0; progressBar1.Maximum = listFiles.Items.Count; progressBar1.Value = 0; } } private void btnExtract_Click(object sender, EventArgs e) { txtOutput.Text = ""; readExcel(); MessageBox.Show("提取完毕!"); } private void readExcel() { ApplicationClass app = new ApplicationClass(); Workbook book = null; Worksheet sheet = null; Range range = null; try { //遍历文件 for (int j = 0; j < listFiles.Items.Count; j++) { try { app.Visible = false; app.ScreenUpdating = false; app.DisplayAlerts = false; // string execPath = Path.Combine(txtFolderPath.Text, (String)listFiles.Items[j]); book = app.Workbooks.Open(execPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value); int count = book.Worksheets.Count; string lineinfo = ""; for (int i = 1; i <= count; i++) { sheet = (Worksheet)book.Worksheets[i]; range = sheet.get_Range("A4", Missing.Value); String rangevalue = (String)range.Value2; rangevalue = rangevalue.Trim(); if ("产品批号".Equals(rangevalue)) { //读取产品批号 lineinfo = ""; range = sheet.get_Range("B4", Missing.Value); lineinfo += range.Value2 + "/t"; //产品规格 range = sheet.get_Range("D4", Missing.Value); lineinfo += range.Value2 + "/t"; //颜色 range = sheet.get_Range("B5", Missing.Value); lineinfo += range.Value2 + "/t"; //抽样日期 range = sheet.get_Range("B6", Missing.Value); lineinfo += range.Value2 + "/r/n"; // txtOutput.Text = txtOutput.Text + lineinfo; } } txtOutput.Text = txtOutput.Text + "/r/n"; // range = null; sheet = null; if (book != null) book.Close(false, Missing.Value, Missing.Value); book = null; } catch (Exception exp) { } // progressBar1.Value = j+1; } } catch (Exception e) { MessageBox.Show(e.Message); } finally { range = null; sheet = null; if (book != null) book.Close(false, Missing.Value, Missing.Value); book = null; if (app != null) app.Quit(); app = null; } } } } |
c# excel工作表及cell读取 .相关推荐
- c# excel工作表及cell读取
昨天晚上做了一回雷锋,帮朋友写了个程序,把他2天都未整理好的Excel数据,一个小时搞定.大致如下:我朋友是搞质检的,每天都会产生一个excel文件,每个excel文件都有20来个工作表,他要做的事情 ...
- SAP 导出 HTML,【我sap这导出数据表格export.mhtml怎么转换为 excel 工作表.xlsx】excel生成html表格数据...
如何把Html网页格式转成Excel表格 1.单击数据--导部数据--新建Web查询. 2.勾选我可以此内容,允许播放,单击继续按钮. 3.在地址栏输入网站地址,转到此网站里面. 4.跳转到指定网页, ...
- 3分钟学会在C ++中以编程方式合并Excel工作表中的单元格
合并和取消合并单元格是Microsoft Excel的一项简单且常用功能.合并单元格可能会在某些情况下很有用,例如,当工作表中有多个列共享相同的标题时,可以合并列上方的单元格以使其具有共同的标题.如果 ...
- 用python的openpyxl库实现对excel工作表的自动化操作
用python的openpyxl库实现对excel工作表的自动化操作 用python的openpyxl库读取excel工作表,批量建立工作表,批量修改工作表标题,批量设置单元格样式,批量调整打印设置. ...
- python中对excel工作表的基础操作:xlrd、xlwt模块笔记
模块介绍 (1)什么是xlrd模块? python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. (2)为什么使用xlrd模块? 在UI自动化 ...
- VBS遍历Excel工作表的方法
这篇文章主要介绍了VBS遍历Excel工作表的实现代码,需要的朋友可以参考下 核心代码 '****************************************** '拖拽文件,获取文件路径 ...
- excel按条件选择工作表_在Excel工作表中选择“实际使用范围”
excel按条件选择工作表 It's easy to select the current range in Excel – just press Ctrl + A. That shortcut se ...
- excel链接隐藏工作表_自动隐藏Excel工作表
excel链接隐藏工作表 When you build a workbook for other people to use, there might be worksheets that can s ...
- 符号在excel中的引用_如何在Excel工作表中添加表情符号
&符号在excel中的引用 In the old days, we typed emoticons in our messages, to show a grin, or wink, etc. ...
最新文章
- 双链表的创建,求长,插入,删除,打印,释放(循环和非循环)
- NYOJ 298 点的变换(矩阵快速幂)
- kafka与zookeeper版本对应关系表
- 简易的bbs论坛开发过程上(后台开发)
- Django项目部署在Centos7
- OPENGGL深度测试
- 【Statistics】均值
- 成都市交委与摩拜科技联手 助推智慧城市建设
- 【渝粤教育】国家开放大学2018年秋季 0222-21T模拟电子电路 参考试题
- 如何通过手机访问本地编写的html页面
- dd wrt php,HG255D(DDWRT)挂载U盘安装emlog和Discuz!教程
- 《游戏设计艺术(第2版)》——学习笔记(9)第9章 游戏为玩家而生
- 至强服务器性能排行,英特尔至强处理器排名天梯 至强cpu天梯2020排名
- 校园锐捷路由器使用指南
- 传奇地图时间限制脚本_地图时间限制脚本
- 专访仙工智能叶杨笙:工业产品如何提升研发效能?
- 程序安装报错0x80070660的解决
- Mac OS下Charles抓包小程序的保姆级操作过程
- 入职阿里啦!字节大牛耗时八个月又一力作,Java面试真题精选
- 蓝桥杯刷题021——填字母游戏(DFS)