在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → excel导入 导出分析源码

excel导入 导出分析源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:1.98M
  • 下载次数:46
  • 浏览次数:334
  • 发布时间:2020-11-03
  • 实例类别:C#语言基础
  • 发 布 人:jian4892
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Excel CE 分析 导入 导出

实例介绍

【实例简介】

【实例截图】ALPS专用报表分析

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Functions;
using System.IO;
using Microsoft.Office.Interop.Excel;

namespace Statistic
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void BTN_SelectFile_Click(object sender, EventArgs e)
        {
            if (this.Fileupload.ShowDialog() == DialogResult.OK)
            {
                string File = this.Fileupload.FileName;
               System.Data.DataTable dt = BLL.GetDataTable(File);
                Lb_information.Text = "共检索出 "   dt.Rows.Count.ToString()   " 数据!";

                if (Package.SelectedIndex == 0)//统计功能!
                {
                    Urgent.AutoGenerateColumns = true;
                    Urgent.DataSource = dt;
                }

            }


        }


        private void Btn_Urgent_Click(object sender, EventArgs e)
        {  
            
            if (Urgent.Rows.Count != 0)
            {
                if (Package.SelectedIndex == 0)//急件统计!
                {
                    //DataTable ReportDT = new DataTable();
                    int sum = Urgent.RowCount-1;
                    int err = 0;
                    Analyse_Progress.Tag = "已启动分析过程请稍等......";
                    Analyse_Progress.Visible = true;//启用进度条
                     Analyse_Progress.Value = 0;
                    Analyse_Progress.Minimum = 0;
                   
                    Analyse_Progress.Maximum = Urgent.RowCount - 1;
                    for (int i = 0; i < sum; i  )
                    {

                        //Line1:确认日期 11/5/2019
                        //Line2:确认时间09:30:11am
                        //Line3:出货日期11/6/2019
                        //line4:出货城市
                        //line7:运输方式
                        //F:n-1 9:30am
                        //L:Hongkong:16:30
                        //L:14:30
                        //S: 9:00am
                        int datecheck = 0;
                        int timecheck = 0;
                        datecheck = BLL.DateDiff(Urgent.Rows[i].Cells[0].Value.ToString(), Urgent.Rows[i].Cells[2].Value.ToString(), 1);
                        if (datecheck == 1)
                        {
                            //判断运输方式
                            string dispatch_type = Urgent.Rows[i].Cells[6].Value.ToString();
                            string city = Urgent.Rows[i].Cells[3].Value.ToString();
                            string time = Urgent.Rows[i].Cells[1].Value.ToString();
                            if (dispatch_type == "F")
                            {
                                timecheck = BLL.comparetime(time, "9:30:00");
                                if (timecheck == 0)
                                {
                                    //Urgent.CurrentCell = null;
                                    //Urgent.Rows[i].Visible = false;
                                     //Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                                    DataGridViewRow row = Urgent.Rows[i];
                                    Urgent.Rows.Remove(row);
                                   sum--;
                                    i--;
                                }
                                else if (timecheck == -1)
                                {
                                    Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                                    err  ;
                                }

                            }
                            else if (dispatch_type == "L")
                            {
                                if (city.Contains("Hong kong") || city.Contains("HONG KONG"))
                                {
                                    timecheck = BLL.comparetime(time, "16:30:00");
                                    if (timecheck == 0)
                                    {
                                        //Urgent.CurrentCell = null;
                                        //Urgent.Rows[i].Visible = false;
                                        //Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                                        DataGridViewRow row = Urgent.Rows[i];
                                        Urgent.Rows.Remove(row);
                                        sum--;
                                        i--;
                                    }
                                    else if (timecheck == -1)
                                    {
                                        Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                                        err  ;
                                    }
                                }
                                else
                                {
                                    timecheck = BLL.comparetime(time, "14:30:00");

                                    if (timecheck == 0)
                                    {
                                        //Urgent.CurrentCell = null;
                                        //Urgent.Rows[i].Visible = false;
                                        //Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                                        DataGridViewRow row = Urgent.Rows[i];
                                        Urgent.Rows.Remove(row);
                                        sum--;
                                        i--;
                                    }
                                    else if (timecheck == -1)
                                    {
                                        Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                                        err  ;
                                    }
                                }

                            }
                            else if (dispatch_type == "S")
                            {
                                timecheck = BLL.comparetime(time, "9:00:00");
                                if (timecheck == 0)
                                {
                                    //Urgent.CurrentCell = null;
                                    //Urgent.Rows[i].Visible = false;
                                    //Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                                    DataGridViewRow row = Urgent.Rows[i];
                                    Urgent.Rows.Remove(row);
                                    sum--;
                                    i--;
                                }
                                else if (timecheck == -1)
                                {
                                    Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                                    err  ;
                                }
                            }
                            else
                            {
                                //Urgent.CurrentCell = null;
                                //Urgent.Rows[i].Visible = false;
                                //Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                                DataGridViewRow row = Urgent.Rows[i];
                                Urgent.Rows.Remove(row);
                                sum--;
                                i--;
                            }
                        }
                        else if (datecheck == 2)
                        {
                            //Urgent.CurrentCell = null;
                            //Urgent.Rows[i].Visible = false;
                            //Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                            DataGridViewRow row = Urgent.Rows[i];
                            Urgent.Rows.Remove(row);
                            sum--;
                            i--;
                        }
                        else if(datecheck ==-1)
                        {
                            Urgent.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                            err  ;
                        }

                        //设置进度条
                        Analyse_Progress.Value  ;
                        //Analyse_Progress.
                    }
                    Analyse_Progress.Visible = false;
                    if (err == 0)
                    {
                        MessageBox.Show("分析完成!");
                        Lb_information.Text = "共分析出 "   (sum - err).ToString()   " 符合条件数据!";
                    }
                    else
                    {
                        Lb_information.Text = "共分析出 "   (sum - err).ToString()   " 符合条件数据!   发现有 "   err.ToString()   " 初始数据有问题!";

                        MessageBox.Show("分析完成!红色底色表示数据有异常!");
                    }

                    BTN_EXPORT.Visible = true;
                }
            }
            else
            {
                MessageBox.Show("请先选择初始文件!");
            }
        }

        private void BTN_EXPORT_Click(object sender, EventArgs e)
        {
            exitToExcel(Urgent, "急件统计报表");
         
        }
       
      public void exitToExcel(DataGridView DGV, string fileName)  //传入DGV与要导出的文件名作为参数
        {
            if (DGV.Rows.Count > 0)
            {

                string saveFileName = fileName;
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel文件|*.xls";
                saveDialog.FileName = saveFileName;
                saveDialog.ShowDialog();
                saveFileName = saveDialog.FileName;
                if (saveFileName.IndexOf(":") < 0)
                    return; //被点了取消 
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                    return;
                }

                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1                                                                                                                    //写入标题
                for (int i = 0; i < DGV.ColumnCount; i  )
                {
                    worksheet.Cells[1, i   1] = DGV.Columns[i].HeaderText;
                }
                //写入数值
                string cellvalue;
                Analyse_Progress.Visible = true;
                Analyse_Progress.Value = 0;
                Analyse_Progress.Minimum = 0;
                Analyse_Progress.Maximum=DGV.Rows.Count-1;
                   
                for (int r = 0; r < DGV.Rows.Count-1; r  )
                {
                    try
                    {
                        for (int i = 0; i < DGV.ColumnCount; i  )
                        {

                            if (DGV.Rows[r].Cells[i].Value.ToString() == null)
                            {
                                cellvalue = string.Empty;
                            }
                            else

                            { 
                               cellvalue =  Convert.ToString(DGV.Rows[r].Cells[i].Value.ToString());
 
                            }

                            worksheet.Cells[r   2, i   1] = cellvalue;
                           
                        }

                        System.Windows.Forms.Application.DoEvents();

                    }
                     
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message);
                    }
                    Analyse_Progress.Value  ;
                }
                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                Analyse_Progress.Visible = false;
                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                      
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show("导出文件时出错,文件可能正被打开!\n"   ex.Message);
                    }

                }

                xlApp.Quit();
                GC.Collect();
                System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
                MessageBox.Show("资料保存成功", "提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("报表为空,无表格需要导出", "提示", MessageBoxButtons.OK);
            }
        }

      

       
    }
    
}

实例下载地址

excel导入 导出分析源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警