海南省BIM中心

 找回密码
 注册请加微信xycost

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2332|回复: 2

C#自动检查目录内的所有EXCEL文件

  [复制链接]

2331

主题

2887

帖子

932万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9324468
发表于 2017-8-18 09:07:54 | 显示全部楼层 |阅读模式
  private void button6_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择广联达导出所在文件夹";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK||dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }

            }

            label4.Text = "清单检查中......";
            string[] files = System.IO.Directory.GetFiles(dialog.SelectedPath, "*.xls", System.IO.SearchOption.AllDirectories);

            for (int i = 0; i < files.Length; i++)
            {
                //if (输入的文件名 == Path.GetFileName(files[i]))
                {
                    string filepath = files[i];
                    exceledit.Open(filepath);
                    listBox1.Items.Add( filepath.Substring(filepath.LastIndexOf("\\") + 1, filepath.Length - 1 - filepath.LastIndexOf("\\")));
                    foreach (Worksheet ws in exceledit.myWorkBook.Worksheets)
                        if (ws.Name.Contains("招-表4"))
                        {
                            exceledit.ActivateSheet(ws.Name);
                            int rowid = 1;

                            while (exceledit.ReadData(rowid, 1).Trim() != "合   计")
                            {
                                int outrow;
                                if (int.TryParse(exceledit.ReadData(rowid, 1), out outrow))
                                {
                                    if (checkBox1.Checked)
                                    {
                                        if (exceledit.ReadData(rowid, 4).Trim() == "")//描述为空
                                            listBox1.Items.Add(exceledit.ReadData(rowid, 1) + "行描述为空" );

                                    }
                                    if (checkBox2.Checked)
                                    {
                                        double result;
                                        if (double.TryParse(exceledit.ReadData(rowid, 7), out result))
                                        {
                                            if (result < 0.01)
                                               listBox1.Items.Add( exceledit.ReadData(rowid, 1)  + "行工程量为空" );
                                        }
                                    }
                                    if (checkBox3.Checked)//价为空
                                    {
                                        double result;
                                        if (double.TryParse(exceledit.ReadData(rowid, 8), out result))
                                        {
                                            if (result < 0.01)
                                                listBox1.Items.Add(exceledit.ReadData(rowid, 1) + "行单价为空" );
                                        }
                                    }

                                }
                                rowid++;
                            }
                        }
                    exceledit.Close();

                    // ...
                    // filename就是你要的值
                }

            }
            label4.Text = "请打开导出后的文件夹";


        }

回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /2 下一条


QQ|小黑屋|手机版|Archiver|ChinaBIM ( 琼ICP备14001050号-1 )

GMT+8, 2024-4-26 00:28 , Processed in 0.033757 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表