DateTable 导出到 Excel

引用博文:https://blog.csdn.net/u013759319/article/details/53394351

引用程序集: Microsoft.Office.Interop.Excel

DateTable dtMRCallBack;

...........省略datetable生成

/*将datatable导入到excel*/
                Excel.Application app =
                    new Excel.Application();
                try
                {
                    app.Visible = false;
                    Excel.Workbook wBook = app.Workbooks.Add(true);
                    Excel.Worksheet wSheet = wBook.Sheets[1];
                    //列头
                    int size = dtMRCallBack.Columns.Count;
                    for (int i = 0; i < size; i++)
                    {
                        wSheet.Cells[1, 1 + i] = dtMRCallBack.Columns[i].ColumnName;
                        wSheet.Cells[1, 1 + i].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//单元格居中
                    }
                    //列内容
                    if(dtMRCallBack.Rows.Count > 0)
                    {
                        for(int i = 0; i < dtMRCallBack.Rows.Count; i++)
                        {
                            for(int j = 0; j < dtMRCallBack.Columns.Count;j++)
                            {
                                string str = dtMRCallBack.Rows[i][j].ToString();
                                wSheet.Cells[i + 2, j + 1] = "'" + str;
                            }
                        }
                    }
                    
                    //自动调整表格
                    Excel.Range allColumn = wSheet.Columns;
                    allColumn.AutoFit();
                    //操作某列水平居中
                    wSheet.Columns[4].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                    //wSheet.Columns.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;表格内容居中

                    //设置禁止弹出保存和覆盖的询问提示框
                    app.DisplayAlerts = false;
                    app.AlertBeforeOverwriting = false;
                    //保存工作簿
                    //wBook.Save();
                    wSheet.SaveAs(saveFileDialog.FileName);
                    wBook.Close();
                    //保存excel文件
                    //app.Save(filePath);
                    //app.SaveWorkspace(filePath);
                    app.Quit();
                    GC.Collect();

                }
                catch(Exception err)//这里还有些问题,比如 对方安装的是WPS 不会提示中文错误,没有安装office 也不会弹出该错误 
                {
                    MessageBox.Show("导出Excel出错!错误原因:" + err.Message,"提示信息",MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                finally
                {
                    if (MessageBox.Show("导出成功,是否打开文件?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        System.Diagnostics.Process.Start(saveFileDialog.FileName);
                }

在用这一段的时候,我发现时间格式的str 会在导出到EXCEL后,会出现自动转换成日期格式并出现‘#######’;

我搜索了资料有两种办法,一种是

Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)wSheet.Cells[1,5];//列5
range.NumberFormat = Microsoft.Office.Interop.Excel.XlParameterDataType.xlParamTypeUnknown;

另一种是:

wSheet.Cells[i + 2, j + 1] = str;  改成
wSheet.Cells[i + 2, j + 1] = “'” + str;

第二种好的一点是不转换为日期格式,而是按照自己想要的格式规范。比如我想要2018-1-15;并不会转成2018/1/15;


引入博文:https://www.cnblogs.com/chendongbky/p/6210712.html

操作单元格样式

worksheet.Cells[1, 1].Value2 = strTitle; //设置单元格内文本  

worksheet.Cells[1, 1].Font.Name = "宋体";//设置字体  

worksheet.Cells[1, 1].Font.Size = 18;//字体大小  

worksheet.Cells[1, 1].Font.Bold = true;//加粗显示  

worksheet.Cells[1, 1].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中  

worksheet.Cells[1, 1].VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中  

worksheet.Cells[1, 1].Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//设置边框  

worksheet.Cells[1, 1].Borders.Weight = Excel.XlBorderWeight.xlMedium;//边框常规粗细  


猜你喜欢

转载自blog.csdn.net/qq_26640897/article/details/80353145