C# 導出Excel方法

private void outPutExcel(System.Data.DataTable dt, System.Data.DataTable dt2)
        {
            if (dt == null) return;
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                //clsLog.m_CreateErrorLog("無法創建Excel對象,可能計算機未安裝Excel", "", "");
                return;
            }
            //創建Excel對象
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            //Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
            Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
            for (int sheetcount = 0; sheetcount < 2; sheetcount++)//循環根據自己需要的sheet的數目這裏是兩個
            {
                if (worksheet == null)
                {
                    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
                }
                else
                {
                    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, worksheet, 1, Type.Missing);
                }
                Microsoft.Office.Interop.Excel.Range range = null;
                if (sheetcount == 0)
                {
                    long totalCount = dt.Rows.Count;
                    long rowRead = 0;
                    float percent = 0;
                    worksheet.Name = "明細資料一";//第一個sheet在Excel中顯示的名稱
                    ////寫入標題
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
                        range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
                        range.Interior.ColorIndex = 15;//背景顏色
                        range.Font.Bold = true;//粗體
                        range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中
                        //加邊框
                        range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
                        range.ColumnWidth = 4.63;//設置列寬
                        range.EntireColumn.AutoFit();//自動調整列寬
                        //r1.EntireRow.AutoFit();//自動調整行高
                    }

                    //寫入內容
                    for (int r = 0; r < dt.Rows.Count; r++)
                    {
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i];
                            range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1];
                            range.Font.Size = 9;//字體大小
                            //加邊框
                            range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
                            range.EntireColumn.AutoFit();//自動調整列寬
                        }
                        rowRead++;
                        percent = ((float)(100 * rowRead)) / totalCount;
                        this.lblCaption.Text = "正在導出數據[" + percent.ToString("0.00") + "%]...";
                        progressBar1.Value = Convert.ToInt32(percent);
                        System.Windows.Forms.Application.DoEvents();
                    }
                    range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
                    if (dt.Columns.Count > 1)
                    {
                        range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
                    }
                }
                else if (sheetcount == 1)
                {
                    long totalCount = dt2.Rows.Count;
                    long rowRead = 0;
                    float percent = 0;
                    worksheet.Name = "建議解決方案";
                    ////寫入標題
                    for (int i = 0; i < dt2.Columns.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = dt2.Columns[i].ColumnName;
                        range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
                        range.Interior.ColorIndex = 15;//背景顏色
                        range.Font.Bold = true;//粗體
                        range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中
                        //加邊框
                        range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
                        range.ColumnWidth = 14.63;//設置列寬
                        range.EntireColumn.AutoFit();//自動調整列寬
                        //r1.EntireRow.AutoFit();//自動調整行高
                    }
                    //寫入內容
                    for (int r = 0; r < dt2.Rows.Count; r++)
                    {
                        for (int i = 0; i < dt2.Columns.Count; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = dt2.Rows[r][i];
                            range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1];
                            range.Font.Size = 9;//字體大小
                            //加邊框
                            range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
                            range.EntireColumn.AutoFit();//自動調整列寬
                        }
                        rowRead++;
                        percent = ((float)(100 * rowRead)) / totalCount;
                        //System.Windows.Forms.Application.DoEvents();
                        this.lblCaption.Text = "正在導出數據[" + percent.ToString("0.00") + "%]...";
                        progressBar1.Value = Convert.ToInt32(percent);
                        System.Windows.Forms.Application.DoEvents();
                    }
                    range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
                    if (dt2.Columns.Count > 1)
                    {
                        range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
                    }
                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.DateTime.Today.ToString("yyyyMMdd"));
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    workbook.SaveAs(path+"aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    

                }
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章