基本信息
源码名称:excel导入 导出分析源码
源码大小:1.98M
文件格式:.zip
开发语言:C#
更新时间:2020-11-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

ALPS专用报表分析

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);
            }
        }

      

       
    }
    
}