C# Excel 基本操作、MS SQL Server

废话不多说,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
namespace 先芯问题点汇总
{
    public partial class 主页面 : Form
    {
        private Excel.Application 标准格式 = new Excel.Application();
        private Excel.Application 欲导入 = new Excel.Application();
        public 主页面()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;
        }

        private void 清空_Click(object sender, EventArgs e)//清空列表
        {
            显示列表.Items.Clear();
        }

        private void 显示列表_DragEnter(object sender, DragEventArgs e)//文件拖入列表后,图标标为Link
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void 显示列表_DragDrop(object sender, DragEventArgs e)//拖拽要转换的文件
        {
            try
            {
                for (int i = 0; i < 20; i++)
                {
                    显示列表.Items.Add(((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(i).ToString());
                }
            }
            catch
            {
 
            }

        }

        private void 显示列表_MouseDoubleClick(object sender, MouseEventArgs e)//双击删除不需要的
        {
            int index = this.显示列表.IndexFromPoint(e.Location);
            try
            {
                    显示列表.Items.RemoveAt(index);
            }
            catch (Exception)
            {
                MessageBox.Show("你这是有多无聊啊!");
            }

        }
        private void 拾取_Click(object sender, EventArgs e)//单击拾取,拾取要转换的文件
        {

            OpenFileDialog GetFile = new OpenFileDialog();
            GetFile.Filter = "Excel 1997-2003(xls)|*.xls|Excel 2007-2016(xlsx)|*.xlsx";
            GetFile.Multiselect = true;
            if (GetFile.ShowDialog() == DialogResult.OK)
            {
                if ((GetFile.OpenFile()) != null)
                {
                    for (int i = 0; i < GetFile.FileNames.Length; i++)
                    {
                        显示列表.Items.Add(GetFile.FileNames[i].ToString());
                    }
                }
            }


        }

        private void 显示列表_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 寻找转换位置函数
        /// </summary>
        /// <returns>上回转换完后的为A几行(大于2)</returns>
        private int TestCell()//
        {
            int j = 3;
            while (标准格式.Cells.Range[单元格.A.ToString() + j.ToString()].Value2 != null)//从第三行开始判断,寻找上一次转换完毕的位置
            {
                j++;

            }

            return j;
        }
        int col=0;

        private void 汇总_Click(object sender, EventArgs e)
        {
            int j=0;
            try
            {
                //Application.StartupPath + @"\" + 
                标准格式.Workbooks.Open(显示列表.Items[0].ToString());
                col = TestCell();//将上一次转换的最后一行位置传入
                标准格式.Visible = true;
                for (int g = 1; g < 显示列表.Items.Count; g++)//从第个文件循环转换,g代表文件数
                {
                    int pp = 0;
                    欲导入.Workbooks.Open(显示列表.Items[g].ToString());//代开第G个文件,准备转换
                    while (欲导入.Cells.Range[单元格.A.ToString() + (pp + 10).ToString()].Value2 != null)//不停地转换,知道待转换文件为空行停止
                    {
                        ///添加边框
                        for (int i=1; i < 8; i++)
                        {
                            标准格式.Cells[j + col, i].Font.Name = this.Font.Name;
                            标准格式.Cells[j + col, i].Borders.Color = Color.Black;
                        }       
                        //
                        标准格式.Cells[j + col, 2] = GetInformation(0, 欲导入.Cells[4, 4].Text);//获取事业部
                        标准格式.Cells[j + col, 3] = 欲导入.Cells[2, 4];//复制客户                    
                        标准格式.Cells[j + col, 4] = 欲导入.Cells[4, 4];//复制产品型号                      
                        标准格式.Cells[j + col, 5] = GetInformation(1, 欲导入.Cells[4, 4].Text);//获取产品阶段        
                        标准格式.Cells[j + col, 6] = 欲导入.Cells[5, 9];//复制任务批量

                        for (int i = 0; i < 10; i++)//从J3行复制到Q3行
                        {
                            CopyCell(j + col, 8 + i, pp + 10, 2 + i);
                            //(行/列,行/列),j表示行数,col表示上一次转换完毕的行数,8+i表示
                            //列数,pp+10表示待转换文件的行数,2+i表示待转换文件的列数
                        }
                        j++;//标准格式行号
                        pp++;//待转换文件非空行
                    };
                }
                for (int i = 0; i + 3 < j + col; i++)//为第一列编号
                {
                    标准格式.Cells.Range[单元格.A.ToString() + (i + 3).ToString()].Value2 = (i + 1).ToString();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            finally//程序转换完毕后退出
            {
                try
                {
                    标准格式.ActiveWorkbook.SaveCopyAs(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\" + @"Issue list summary " + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
                }
                catch(Exception )
                {
                    标准格式.ActiveWorkbook.SaveCopyAs(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\" + @"Issue list summary " + DateTime.Now.ToString("yyyy-MM-dd")+("附件") + ".xls");
                }
                MessageBox.Show("文件已经保存到桌面\n单击确定程序退出");
                欲导入.Quit();
                标准格式.DisplayAlerts = false;
                标准格式.ActiveWorkbook.Close();
                标准格式.Quit();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                this.Close();
            }
        }
        string Add = string.Format(@"server=数据库地址;database=数据库名称;uid=账户名;pwd=密码");//数据库登陆地址,数据库名称,登陆用户名:SA,密码lierda
        private string GetInformation(int Colum, string 型号)
        {
            string 事业部 = "";
            try
            {
                DataSet DS = new DataSet();
                SqlConnection Con = new SqlConnection(Add);
                string sle = string.Format(@"select 事业部,产品阶段 from LierdaProductMasterList where 先芯料号 like '%{0}%' ", 型号);
                SqlDataAdapter SDA = new SqlDataAdapter(sle, Con);
                SDA.Fill(DS);
                事业部 = DS.Tables[0].Rows[0][Colum].ToString();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            
            return 事业部;
        }
        
        /// <summary>
        /// 将待汇总单元格汇总到表中
        /// </summary>
        /// <param name="k">标准格式“行”</param>
        /// <param name="number1">标准格式“列”</param>
        /// <param name="j">待汇总“行”</param>
        /// <param name="number2">待汇总列</param>
        private void CopyCell(int k,int number1,int j,int number2)
        {
            try
            {
                Excel.Range Copycell = 欲导入.Cells[j, number2];
                Copycell.Copy();
                标准格式.Cells[k, number1].PasteSpecial(Excel.XlPasteType.xlPasteFormats);
            }
            catch(Exception)
            {
                try
                {
                    Excel.Range Copycell = 欲导入.Cells[j, number2];
                    Copycell.Copy();
                    标准格式.Cells[k, number1].PasteSpecial(Excel.XlPasteType.xlPasteFormats);

                }
                catch (Exception)
                {
                    try
                    {
                        Excel.Range Copycell = 欲导入.Cells[j, number2];
                        Copycell.Copy();
                        标准格式.Cells[k, number1].PasteSpecial(Excel.XlPasteType.xlPasteFormats);

                    }
                    catch (Exception err)
                    {
                        MessageBox.Show("程序异常" + err.Message);
                    }
                }
            }
        }
        private enum 单元格//列号
        {
           A=1,
           B=2,
           C=3,
           D=4,
           E=5,
           F=6,
           G=7,
           H=8,
           I=9,
           J=10,
           K=11,
           L=12,
           M=13
          


        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            try
            {
                string filename = 显示列表.Items[1].ToString();
                this.Text = filename.Substring(filename.LastIndexOf(@"\") + 1, filename.LastIndexOf("Trial Run Report") - filename.LastIndexOf(@"\") - 2);
            }
            catch
            {
                this.Text = "请拖入或拾取要汇总的文件";
            }
        }
        /// <summary>
        /// 保存并推出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存_Click(object sender, EventArgs e)
        {
            标准格式.Workbooks.Close();
            标准格式.Quit();
            欲导入.Workbooks.Close();
            欲导入.Quit();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            this.Close();
        }

    }
}
标准格式文档:<img src="https://img-blog.csdn.net/20160121102241449" alt="" />
待导入文档格式:<img src="https://img-blog.csdn.net/20160121102503655" alt="" />

转换完毕后的效果:

软件操作界面:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值