【xiaomi】ASP.NET 分Sheet匯出EXCEL 2003

iDotNetSpace發表於2008-07-08
EXCEL 2003有65536行資料的限制,而企業資料往往超過65536行,多於65536行的Excel匯出經常形成困擾。其實,只要一個簡單的迴圈加一點點對Sheet操作的技巧,即可實現大於65536行資料分Sheet匯出成Excel的效果。
首先,引用Excel.dll檔案:
/Files/xiaomi7732/Excel.rar
  然後,引用名稱空間:using Excel;
  最後,新增方法:
        /// 
        
/// ASP.NET,分Sheet匯出Excel檔案
        
/// 
        
/// 用於匯出的DataView
        
/// 匯出的資料夾,例如~/ExcelDownload/
        
/// 檔名,例如test.xls
        
/// Sheet的名稱,如果匯出多個Sheet,會自動在名稱後面加1、2、3
        
/// 每個Sheet包含的資料行數,此數值不包括標題行。所以,對於65536行資料,請將此值設定為65535
        
/// 匯出完成後,是否給資料加上邊框線

        public static void WebExportToExcel(DataView dv, string tmpExpDir, string refFileName, string sheetName, int sheetSize, bool setBorderLine)
        
{
            
//設定多少行為一個Sheet
            int RowsToDivideSheet = sheetSize;
            
//計算Sheet數
            int sheetCount = (dv.Table.Rows.Count - 1/ RowsToDivideSheet + 1;

            GC.Collect();
            Application excel;
            _Workbook xBk;
            _Worksheet xSt
=null;
            excel 
= new ApplicationClass();
            xBk 
= excel.Workbooks.Add(true);

            
//申明迴圈中要使用的變數
                int dvRowStart;
                
int dvRowEnd;
                
int rowIndex = 0;
                
int colIndex = 0;

            
//對全部Sheet進行操作
            for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
            
{
                
//初始化Sheet中的變數
                rowIndex = 1;
                colIndex 
= 1;
                
//計算起始行
                dvRowStart = sheetIndex * RowsToDivideSheet;
                dvRowEnd 
= dvRowStart + RowsToDivideSheet-1;
                
if (dvRowEnd > dv.Table.Rows.Count-1)
                
{
                    dvRowEnd 
= dv.Table.Rows.Count - 1;
                }


                
//建立一個Sheet
                if (null == xSt)
                
{
                    xSt 
= (_Worksheet)xBk.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
                }

                
else
                
{
                    xSt 
= (_Worksheet)xBk.Worksheets.Add(Type.Missing, xSt, 1, Type.Missing);
                }

                
                
//設定SheetName
                xSt.Name = sheetName;
                
if (sheetCount > 1)
                
{
                    xSt.Name 
+= ((int)(sheetIndex + 1)).ToString();
                }


                
//取得標題
                foreach (DataColumn col in dv.Table.Columns)
                
{
                    
//設定標題格式
                    xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //設定標題居中對齊
                    xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;
                    
//填值,並進行下一列
                    excel.Cells[rowIndex, colIndex++= col.ColumnName;
                }

                
                
//取得表格中數量
                int drvIndex;
                
for(drvIndex=dvRowStart;drvIndex<=dvRowEnd;drvIndex++)
                
{
                    DataRowView row
=dv[drvIndex];
                    
//新起一行,當前單元格移至行首
                    rowIndex++;
                    colIndex 
= 1;
                    
foreach (DataColumn col in dv.Table.Columns)
                    
{
                        
if (col.DataType == System.Type.GetType("System.DateTime"))
                        
{
                            excel.Cells[rowIndex, colIndex] 
= (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                        }

                        
else if (col.DataType == System.Type.GetType("System.String"))
                        
{
                            excel.Cells[rowIndex, colIndex] 
= "'" + row[col.ColumnName].ToString();
                        }

                        
else
                        
{
                            excel.Cells[rowIndex, colIndex] 
= row[col.ColumnName].ToString();
                        }

                        colIndex
++;
                    }

                }

                
//使用最佳寬度
                Range allDataWithTitleRange = xSt.get_Range(excel.Cells[11], excel.Cells[rowIndex, colIndex-1]);
                allDataWithTitleRange.Select();
                allDataWithTitleRange.Columns.AutoFit();
                
//xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex, colIndex-1]).Columns.AutoFit();
                if (setBorderLine)
                
{
                    allDataWithTitleRange.Borders.LineStyle 
= 1;  
                }

            }


            
            
//excel.Visible = true;
            string absFileName = HttpContext.Current.Server.MapPath(System.IO.Path.Combine(tmpExpDir, refFileName));
            xBk.SaveCopyAs(absFileName);

            xBk.Close(
falsenullnull);

            excel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);

            xBk 
= null;
            excel 
= null;
            xSt 
= null;
            GC.Collect();
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-374431/,如需轉載,請註明出處,否則將追究法律責任。

相關文章