日期:2014-05-18 浏览次数:20885 次
 private void btnLogReport_Click(object sender, EventArgs e)
        {
            //建立Excel对象
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Application.Workbooks.Add(true);
            //生成字段名称
            for (int i = 0; i < dataGridView_LogReport.ColumnCount; i++)
            {
                excel.Cells[1, i + 1] = dataGridView_LogReport.Columns[i].HeaderText;
            }
            //填充数据
            for (int i = 0; i < dataGridView_LogReport.RowCount - 1; i++)
            {
                for (int j = 0; j < dataGridView_LogReport.ColumnCount; j++)
                {
                    if (dataGridView_LogReport[j, i].Value == typeof(string))
                    {
                        excel.Cells[i + 2, j + 1] = "" + dataGridView_LogReport[i, j].Value.ToString();
                    }
                    else
                    {
                        excel.Cells[i + 2, j + 1] = dataGridView_LogReport[j, i].Value.ToString();
                    }
                }
            }
            excel.Visible = true;
        }
Range DetailRange = wSheet.Range[wSheet.Cells[StartRow, 1], wSheet.Cells[LastRowIndex, this.reportColumnCount]];
DetailRange.Columns.AutoFit();