关于datagirdview转化成Excel的问题
我按照下面的代码可以将 datagirdview 中的内容写到Excel里面,但是有个缺点就是datagirdview表中有些数据太长了,转到Excel中的时候就变成了 ###### ,要自己手动调整一下Excel中的表格宽度才能显示 ##### 中的内容,那我有什么办法才能使得不用手动调整宽度,让程序自己调整啊??
小弟新生,请详细!!
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();