日期:2014-05-18  浏览次数:20912 次

winform DataTable数据导入excel
小弟在网上搜索了一大把DataTable数据导入excel例子,也实现了该功能,但是有个问题小问题始终未能解决,就是某列
的数字类型太长,比如身份证号,在excel里显示就是指数类型,请问这个如何修改
  代码如下:
 
C# code

 private static bool ExportExcel()
        {
            if (saveFileName.IndexOf(":") < 0) return false; //被点了取消

            Excel.Application xlApp = new Excel.Application();
            object missing = System.Reflection.Missing.Value;

            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                return false;
            }
            Excel.Workbooks workbooks = xlApp.Workbooks;
            Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];    //取得sheet1
            Excel.Range range;

            string strCaption = saveFileName.Remove(0, saveFileName.LastIndexOf('\\') + 1);
            strCaption = strCaption.Remove(strCaption.Length - 4, 4);

            long totalCount = dtDataSource.Rows.Count;
            long rowRead = 0;
            float percent = 0;


            range = (Excel.Range)worksheet.get_Range(worksheet.Cells[1, 1],
                worksheet.Cells[2, dtDataSource.Columns.Count]);                    //标题占用前两行
            range.Merge(missing);                                                   //合并
            range.Font.Bold = true;                                                 //粗体设置
            range.Font.Size = 16;                                                   //字体大小设置 
            range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;              //水平对齐设置 
            range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;                //垂直对齐设置
            //range.FormulaR1C1 = 公式;             //公式设置
            //range.ColumnWidth = 宽度;             //列宽设置
            //range.RowHeight = 行高;               //行高  
            worksheet.Cells[1, 1] = strCaption;

            //写入字段
            for (int i = 0; i < dtDataSource.Columns.Count; i++)
            {
                worksheet.Cells[4, i + 1] = dtDataSource.Columns[i].ColumnName;
                range = (Excel.Range)worksheet.Cells[4, i + 1];
                range.Interior.ColorIndex = 15;
                range.Font.Bold = true;
            }

            //写入数值
            for (int r = 0; r < dtDataSource.Rows.Count; r++)
            {
                for (int i = 0; i < dtDataSource.Columns.Count; i++)
                {
                    worksheet.Cells[r + 5, i + 1] = dtDataSource.Rows[r][i];
                }
                rowRead++;
                percent = ((float)(100 * rowRead)) / totalCount;
                Application.DoEvents();
            }
            worksheet.SaveAs(saveFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);

            range = worksheet.get_Range(worksheet.Cells[4, 1],
                worksheet.Cells[dtDataSource.Rows.Count + 4, dtDataSource.Columns.Count]);
            range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
            range.NumberFormat = "@"; //这句话是我在上面搜索到的,可惜不起作用
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;

            if (dtDataSource.Columns.Count > 1)
            {
                range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
            }
            workbook.Close(missing, missing, missing);