c# excel工作表及cell读取
来源:互联网 发布:7u分享网络官网下载 编辑:程序博客网 时间:2024/06/04 17:42
昨天晚上做了一回雷锋,帮朋友写了个程序,把他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读取 .
- C# 读取Excel工作表
- C#读取excel表
- C# 读取excel表
- asponse.cell读取excel内容
- C#锁定EXCEL工作表
- C#锁定EXCEL工作表
- C#读取excel工作表(sheet)中的内容-----采用名称的方法
- C#读取EXCEL中工作表不是Sheet1等默认名称
- 破解EXCEL工作表保护密码及宏保护密码,读取隐藏工作表内的数据
- C# 读取 excel 中 sheet 及数据到DataTable
- C# 读取Excel表格出现空白行原因及解决办法
- C#第三次作业:Excel数据读取 及 HTML文件初步
- 第三次C#作业-Excel数据读取及HTML文件初步
- C#第三次作业 Excel数据读取 及 HTML文件初步
- C#第三次作业:Excel数据读取 及 HTML文件初步
- C#第三次作业 Excel数据读取 及 HTML文件初步
- java日志系统学习--java.util.log.Level
- 注册、反注册dll文件,regsvr32命令详解
- eclipse版本
- 安装debian,遭遇PCI:NOT using mmconfig 错误,刷新BIOS
- VC++判断一个进程是否是64位
- c# excel工作表及cell读取
- txt文件切割器
- 1080 奇怪的数列
- 统计文本行
- 深入分析 iBATIS 框架之系统架构与映射原理
- 适配器模式
- 郁闷了半天
- C/C++从入门到高手所有必备PDF书籍收藏
- 摘录:网页form表单重复提交的问题