我想在窗口应用程序中使用标题徽标和文本将datagridview导出为ex​​cel [英] I want to export datagridview to excel with header logo and text in window application

查看:54
本文介绍了我想在窗口应用程序中使用标题徽标和文本将datagridview导出为ex​​cel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在窗口应用程序中将datagridview导出为带有标题徽标和文本的excel



我尝试过:



i want to export datagridview to excel with header logo and text in window application

What I have tried:

Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Application oExcel_12 = null;                //Excel_12 Application
            Microsoft.Office.Interop.Excel.Workbook oBook = null;                       // Excel_12 Workbook
            Microsoft.Office.Interop.Excel.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
            Microsoft.Office.Interop.Excel.Worksheet oSheet = null;                     // Excel_12 Worksheet
            Microsoft.Office.Interop.Excel.Range oRange = null;                         // Cell or Range in worksheet
            Object oMissing = System.Reflection.Missing.Value;
            xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

            // Create an instance of Excel_12.
            oExcel_12 = new Microsoft.Office.Interop.Excel.Application();

            // Make Excel_12 visible to the user.
            oExcel_12.Visible = true;

            // Set the UserControl property so Excel_12 won't shut down.
            oExcel_12.UserControl = true;

            // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");

            // Add a workbook.
            oBook = oExcel_12.Workbooks.Add(oMissing);

            // Get worksheets collection 
            oSheetsColl = oExcel_12.Worksheets;

            // Get Worksheet "Sheet1"
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheetsColl.get_Item(1);
            int count = dGV.Columns.Count;
            Microsoft.Office.Interop.Excel.Range formatRange = oSheet.UsedRange;
            formatRange = oSheet.get_Range("a1") as Microsoft.Office.Interop.Excel.Range;
            formatRange.EntireRow.Font.Bold = true;
            oSheet.Cells[1, 1] = "Bold";
            oRange = oSheet.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range;
            oRange.Value2 = "Agent  : " + cmbagent.Text + " GST Type : " + cmbGSTType.Text;
            // Export titles
            for (int j = 0; j < dGV.Columns.Count; j++)
            {
                Microsoft.Office.Interop.Excel.Range formatRange2 = oSheet.UsedRange;
                formatRange2 = oSheet.get_Range("b1") as Microsoft.Office.Interop.Excel.Range;
                formatRange2.EntireRow.Font.Bold = true;
                oSheet.Cells[2, j + 1] = "Bold";
                oRange = oSheet.Cells[2, j + 1] as Microsoft.Office.Interop.Excel.Range;
                oRange.Value2 = dGV.Columns[j].HeaderText;
            }
            // Export data
            for (int i = 0; i < dGV.Rows.Count; i++)
            {
                for (int j = 0; j < dGV.Columns.Count; j++)
                {
                    oRange = oSheet.Cells[i + 3, j + 1] as Microsoft.Office.Interop.Excel.Range;
                    oRange.Value2 = dGV.Rows[i].Cells[j].Value;
                }
            }
            oRange.get_Range("A1", "A2").Merge(false);

            // Release the variables.
            //oBook.Close(false, oMissing, oMissing);
            oBook = null;

            //oExcel_12.Quit();
            oExcel_12 = null;

            // Collect garbage.
            GC.Collect();

推荐答案

查看此网站

如何从C#app将图片插入Excel? - 堆栈溢出 [ ^ ]


这篇关于我想在窗口应用程序中使用标题徽标和文本将datagridview导出为ex​​cel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆