博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

     #region 导出excel方法

        public static void ExportTexcel(System.Data.DataTable dt)
        {
            System.Data.DataTable data = dt;
            data.Columns.Add("特殊说明");
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "Execl files (*.xls)|*.xls";
            saveDlg.FilterIndex = 0;
            saveDlg.RestoreDirectory = true;
            saveDlg.CreatePrompt = true;
            saveDlg.Title = "导出文件保存路径";


            saveDlg.FileName = "新建Excel文档";

            if (DialogResult.OK == saveDlg.ShowDialog())
            {
                try
                {

                    string strName = saveDlg.FileName;
                    if (strName.Length != 0)
                    {

                        System.Reflection.Missing miss = System.Reflection.Missing.Value;
                        Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                        excel.Application.Workbooks.Add(true); ;
                        excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
                        if (excel == null)
                        {
                            MessageBox.Show("EXCEL无法启动!""错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
                        Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
                        Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
                        sheet.Name = "质检报表";

                        int rowIndex = 4;
                        int colIndex = 0;
                        //取得列标题 
                        foreach (DataColumn col in data.Columns)
                        {
                            colIndex++;
                            excel.Cells[4, colIndex] = col.ColumnName;

                            //设置标题格式为居中对齐
                            sheet.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).Font.Bold = true;
                            sheet.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                            sheet.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).Select();

                            //sheet.get_Range(excel.Cells[4, colIndex], excel.Cells[4, colIndex]).Interior.ColorIndex = 19;//19;//设置为浅黄色,共计有56种
                        }

                        //取得表格中的数据 
                        foreach (DataRow row in data.Rows)
                        {
                            rowIndex++;
                            colIndex = 0;
                            foreach (DataColumn col in data.Columns)
                            {
                                colIndex++;
                                if (col.DataType == System.Type.GetType("System.DateTime"))
                                {
                                    excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                                    sheet.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
                                }
                                else
                                    if (col.DataType == System.Type.GetType("System.String"))
                                    {
                                        excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
                                        sheet.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
                                    }
                                    else
                                    {
                                        excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                                    }
                            }
                        }

                        int rowSum = rowIndex + 1;

                        //取得整个报表的标题 
                        excel.Cells[22] = "质检报表";
                        excel.Cells[36] = "日期:"+ DateTime.Now.ToString("yyyy年MM月dd日");
                        excel.Cells[rowIndex + 41] = "哈尔滨xx通信软件开发有限公司";
                        excel.Cells[rowIndex + 54] = "稽查人员:";
                        excel.Cells[rowIndex + 55] = "________";
                        excel.Cells[rowIndex + 56] = "负责人员:";
                        excel.Cells[rowIndex + 57] = "________";

                        //设置整个报表的标题格式 
                        sheet.get_Range(excel.Cells[22], excel.Cells[22]).Font.Bold = true;
                        sheet.get_Range(excel.Cells[22], excel.Cells[22]).Font.Size = 22;

                        //设置报表表格为最适应宽度 
                        sheet.get_Range(excel.Cells[42], excel.Cells[rowSum, colIndex]).Select();
                        sheet.get_Range(excel.Cells[42], excel.Cells[rowSum, colIndex]).Columns.AutoFit();

                        //设置整个报表的标题为跨列居中 
                        sheet.get_Range(excel.Cells[22], excel.Cells[2, colIndex]).Select();
                        sheet.get_Range(excel.Cells[22], excel.Cells[2, colIndex]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenterAcrossSelection;

                        //绘制边框 
                        sheet.get_Range(excel.Cells[41], excel.Cells[rowSum, colIndex]).Borders.LineStyle = 1;
                        sheet.get_Range(excel.Cells[41], excel.Cells[rowSum, 1]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;//设置左边线
                        sheet.get_Range(excel.Cells[41], excel.Cells[4, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;//设置上边线
                        sheet.get_Range(excel.Cells[4, colIndex], excel.Cells[rowSum, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;//设置右边线
                        sheet.get_Range(excel.Cells[rowSum, 1], excel.Cells[rowSum, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;//设置下边线

 


                        sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
                        book.Close(false, miss, miss);
                        books.Close();
                        excel.Quit();

                        System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

                        GC.Collect();
                        MessageBox.Show("数据已经成功导出到:" + saveDlg.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //toolStripProgressBar1.Value = 0;
                        
//toolStripProgressBar1.Visible = false;
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        #endregion