开发者

Is there a way to automatically generate a datagridview that is not button controlled

Currently how my code works is I click button 1 and it generates a datagridview, then click button 2 it passes it to excel. Is their a way to eliminate the button controls and just have the code automatically do these things?

 **private void button1_Click(object sender, EventArgs e)**
    {
        string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
        foreach (string fileName in fileEntries)
        {

            var query = from file in fileEntries
                        let doc = XDocument.Load(file)
                        let x = doc.Descendants("XAxisCalib").Single()
                        let y = doc.Descendants("YAxisCalib").Single()
                        let z = doc.Descendants("ZAxisCalib").Single()
                        select new
                        {

                            XMax = x.Element("Max").Value,
                            XMin = x.Element("Min").Value,
                            YMax = y.Element("Max").Value,
                            YMin = y.Element("Min").Value,
                            ZMax = z.Element("Max").Value,
                            ZMin = z.Element("Min").Value
                        }; 


            var bs3 = new BindingSource { DataSource = query };

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;

            dataGridView1.DataSource = bs3;
        }
    }



    **private void button2_Click(object sender, EventArgs e)**
    {
        Excel.Application xlApp ;
        Excel.Workbook xlWorkBook ;
        Excel.Worksheet xlWorkSheet ;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        int i = 0;
        int j = 0;

        for (i = 0; i <= dataGridView1.RowCount  - 1; i++)
        {
            for (j = 0; j <= dataGridView1.ColumnCount  - 1; j++)
            {
                DataGridViewCell cell = dataGridView1[j, i];
                xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
            }


        }

        Excel.Range chartRange; 

        Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
        Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
        Excel.Chart chartPage = myChart.Chart;

        chartRange = xlWorkSheet.get_Range("A1", "B65");
        chartPage.SetSourceData(chartRange, misValue);
        chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

        Excel.Range chartR开发者_如何学编程ange1;

        Excel.ChartObjects xlChart1 = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
        Excel.ChartObject myChart1 = (Excel.ChartObject)xlChart1.Add(10, 80, 300, 250);
        Excel.Chart chartPage1 = myChart1.Chart;

        chartRange1 = xlWorkSheet.get_Range("C1", "D65");
        chartPage1.SetSourceData(chartRange1, misValue);
        chartPage1.ChartType = Excel.XlChartType.xlColumnClustered;

        Excel.Range chartRange2;

        Excel.ChartObjects xlChart = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
        Excel.ChartObject myChart2 = (Excel.ChartObject)xlChart.Add(10, 80, 300, 250);
        Excel.Chart chartPage2 = myChart2.Chart;

        chartRange2 = xlWorkSheet.get_Range("E1", "F65");
        chartPage2.SetSourceData(chartRange2, misValue);
        chartPage2.ChartType = Excel.XlChartType.xlColumnClustered;

        xlWorkBook.SaveAs("ScicloneMaxMin.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);


You can put your code in the form's constructor or a Shown handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜