C#中GDI+导出excel (含图片, 数据)

来源:互联网 发布:铃声放大软件摩托罗拉 编辑:程序博客网 时间:2024/06/01 10:25

using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using Excel = Microsoft.Office.Interop.Excel;


private void toolStripButton2_Click(object sender, EventArgs e) //导出excel
{
    if (dataGridView1.Rows.Count == 0) return;  //没有数据的话,不要往下执行

    if (MessageBox.Show("真的要将数据导出到Excel吗?", "数据导出", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        try
        {
            string modpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "modelv1.xls"); //特定的文件模板,此文件已经做好,并且放在特定的目录下,命名为 modelv1.xls
            string tmppath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "tmpv1.xls"); //C#生成的文件保存在 MyDocuments 目录下,命名为 tmpv1.xls
            FileInfo mod = new FileInfo(modpath); //文件复印,修改,删除等方法
            FileInfo tmp = new FileInfo(tmppath);
            tmp.Delete(); //如果有存在,先删除历史档案
            mod.CopyTo(tmppath); //文件的复制

            Excel.Application tmpExcel = new Excel.ApplicationClass();
            tmpExcel.Visible = false;

            Excel.Workbook tmpbook = tmpExcel.Workbooks.Open(tmppath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);  //表格的权限设定,如只读,密码,权限
            Excel.Worksheet tmpsheet = (Excel.Worksheet)tmpbook.Sheets["重量"]; //工作表的表名
            tmpsheet.Cells[1, 3] = dataGridView1[0, 0].Value.ToString().Trim(); //制样单号
            Microsoft.Office.Interop.Excel.Application exc = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Worksheet ws = new Microsoft.Office.Interop.Excel.Worksheet();
            exc.Application.Workbooks.Add(true);
            Microsoft.Office.Interop.Excel._Workbook wk = exc.Workbooks.get_Item(1);
            Microsoft.Office.Interop.Excel.Sheets wss = (Microsoft.Office.Interop.Excel.Sheets)wk.Worksheets;
            Microsoft.Office.Interop.Excel._Worksheet _ws = (Microsoft.Office.Interop.Excel._Worksheet)(wss.get_Item(1));
            object m_objOpt = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Pictures pic = (Microsoft.Office.Interop.Excel.Pictures)_ws.Pictures(m_objOpt);

            Bitmap bit = new Bitmap(PrintReportImage());  //PrintReportImage 自己写的函数
            //bit.Save(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            //pic.Insert(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "1.bmp", m_objOpt);
            string tmppath1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "1.jpg"); //文件保存在 MyDocuments 目录下,命名为 1.jpg
            bit.Save(tmppath1, System.Drawing.Imaging.ImageFormat.Jpeg);//文件格式 jpeg

            //以下是添加新的sheet(不同的数据放在不同的sheet中)
            Excel.Worksheet objsheet = (Excel.Worksheet)tmpbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing);
            //以下是将图片插入到excel中
            Excel.Pictures thepic = objsheet.Pictures(Type.Missing) as Excel.Pictures;
            thepic.Insert(tmppath1, Type.Missing);

            tmpExcel.Visible = true; //导入的excel可以显示
            tmpExcel.DisplayAlerts = false;
            tmpExcel.AlertBeforeOverwriting = false;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "错误提示");
        }
    }
}

原创粉丝点击