QQ:929412592 

[C#][代码收集] - Winform

[C#][代码收集] - Winform [编辑]


   01、 Winform导出Excel

   02、 WinForm 控件之Image 

   03、 >

   04、 >

   05、 >

   06、 >

   07、 >

   08、 >

   09、 >

   10、 > 


 01、Winform导出Excel 目录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace SaleApplication.Class
{
    class ExportExcelOfWinform
    {
        public static bool OutToExcelFromDataGridView(string title, DataGridView dgv, bool isShowExcel)
        {
            int titleColumnSpan = 0;//标题的跨列数
            string fileName = "";//保存的excel文件名
            int columnIndex = 1;//列索引
            if (dgv.Rows.Count == 0) return false;
            
            /*保存对话框*/
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "导出Excel(*.xls)|*.xls";
            sfd.FileName = title + DateTime.Now.ToString("yyyyMMddhhmmss");

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fileName = sfd.FileName;
                /*建立Excel对象*/
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                if (excel == null)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel!");
                    return false;
                }
                try
                {
                    excel.Application.Workbooks.Add(true);
                    excel.Visible = isShowExcel;
                    /*分析标题的跨列数*/
                    foreach (DataGridViewColumn column in dgv.Columns)
                    {
                        if (column.Visible == true)
                        titleColumnSpan++;
                    }
                    /*合并标题单元格*/
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
                    //worksheet.get_Range("A1", "C10").Merge(); 
                    worksheet.get_Range(worksheet.Cells[1, 1] as Range, worksheet.Cells[1, titleColumnSpan] as Range).Merge();
                    /*生成标题*/
                    excel.Cells[1, 1] = title;
                    (excel.Cells[1, 1] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//标题居中
                    //生成字段名称
                    columnIndex = 1;
                    for (int i = 0; i < dgv.ColumnCount; i++)
                    {
                        if (dgv.Columns[i].Visible == true)
                        {
                            excel.Cells[2, columnIndex] = dgv.Columns[i].HeaderText;
                            (excel.Cells[2, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//字段居中
                            columnIndex++;
                        }
                    }
                    //填充数据 
                    for (int i = 0; i < dgv.RowCount; i++)
                    {
                        columnIndex = 1;
                        for (int j = 0; j < dgv.ColumnCount; j++)
                        {
                            if (dgv.Columns[j].Visible == true)
                            {
                                if (dgv[j, i].ValueType == typeof(string))
                                {
                                    excel.Cells[i + 3, columnIndex] = "'" + dgv[j, i].Value.ToString();
                                }
                                else
                                {
                                    excel.Cells[i + 3, columnIndex] = dgv[j, i].Value.ToString();
                                }
                            
                                (excel.Cells[i + 3, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignLeft;//字段居中
                                columnIndex++;
                            }
                        }
                    }
                    worksheet.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
                }
                catch
                { 
                    
                }
                finally
                { 
                    excel.Quit(); 
                    excel = null;
                    GC.Collect();
                }
                
                //KillProcess("Excel");
                return true;
            }
            else
            {
                return false;
            }
        }
        
        private static void KillProcess(string processName)//杀死与Excel相关的进程
        {
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();//得到所有打开的进程
            try
            {
                foreach(System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
                {
                    if(!thisproc.CloseMainWindow())
                    {
                        thisproc.Kill();
                    }
                }
            }
            catch(Exception Exc)
            {
                throw new Exception("", Exc);
            }
        }

    }
}
查看代码

   02、WinForm 控件之Image 目录

用timer控件 

private void timer1_Tick(object sender, EventArgs e)
{

    this.Opacity+=0.2;
    this.Width = this.Width + 20;
    this.Height = this.Height + 20;

    if (this.Opacity == 1) 
    { 
        timer1.Enabled = false; 
    }
}

private int temp = 0;ImageList ilist = new ImageList(); 


private void timer1_Tick(object sender, EventArgs e) 
{ 
    if (temp % 3 != 0) 
    { 
        pictureBox1.Image = ilist.Images[temp++]; 
    } 
    else 
    { 
        temp = 1; pictureBox1.Image = ilist.Images[0]; 
    } 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    timer1.Enabled=true; 
    ilist.Images.Add(Image.FromFile(@"X:\\XXX\\1.jpg")); 
    ilist.Images.Add(Image.FromFile(@"X:\\XXX\\2.jpg")); 
    ilist.Images.Add(Image.FromFile(@"X:\\XXX\\3.jpg")); 
    ilist.Images.Add(Image.FromFile(@"X:\\XXX\\4.jpg")); 
    
    this.pictureBox1.Image = ilist.Images[0]; 
}
查看代码
PictureBoxSizeMode.Normal:       默认情况下,在 Normal 模式中,Image 置于 PictureBox 的左上角,凡是因过大而不适合 PictureBox 的任何图像部分都将被剪裁掉。
PictureBoxSizeMode.StretchImage: 使用 StretchImage 值会使图像拉伸或收缩,以便适合 PictureBox。
PictureBoxSizeMode.AutoSize:     使用 AutoSize 值会使控件调整大小,以便总是适合图像的大小。
PictureBoxSizeMode.CenterImage:  使用 CenterImage 值会使图像居于工作区的中心。
PictureBoxSizeMode.Zoom:         使用 Zoom 的值可以使图像被拉伸或收缩以适应 PictureBox;但是仍然保持原始纵横比。
PictureBoxSizeMode 属性

   03、目录

 

   04、目录

 

   05、目录

 

   06、目录

 

   07、目录

 

   08、目录

 

   09、目录

 

   10、目录

 
posted @ 2017-10-13 15:58  辉创1989  阅读(182)  评论(0编辑  收藏  举报