是否有一种方法可以自动生成不受按钮控制的datagridview ?
本文关键字:自动生成 按钮 控制 datagridview 方法 一种 是否 | 更新日期: 2023-09-27 18:08:29
目前我的代码是如何工作的,我点击按钮1,它生成一个datagridview,然后点击按钮2它传递给excel。这是一种消除按钮控制的方法,只是让代码自动完成这些事情吗?
**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 chartRange1;
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);
您可以将代码放在表单的构造函数或Shown
处理程序中。