首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于datagirdview转化成Excel的有关问题

2012-03-31 
关于datagirdview转化成Excel的问题我按照下面的代码可以将 datagirdview 中的内容写到Excel里面,但是有个

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

C# code
 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;        }


[解决办法]
参考代码如下:
C# code
Range DetailRange = wSheet.Range[wSheet.Cells[StartRow, 1], wSheet.Cells[LastRowIndex, this.reportColumnCount]];DetailRange.Columns.AutoFit(); 

热点排行