C# Excel Control (엑셀 쓰기 기능) 대용량

in csarp •  7 years ago  (edited)

엑셀 몇만줄의 데이터를 쓰고 내려받을때 주료 사용합니다.

 private void TOExcel(DataTable dt, string path)
  {

        Excel.Application appliCation;
        Excel.Workbook workBook;
        Excel.Worksheet workSheet;

        try
        {
            appliCation = new Excel.Application();
            workBook = (Excel.Workbook)(appliCation.Workbooks.Add(true));
            workSheet = (Excel.Worksheet)workBook.ActiveSheet;
            appliCation.Visible = false;
            appliCation.UserControl = false;
            object TypMissing = Type.Missing;
            int iRow = 0;
            string[] headers = new string[dt.Columns.Count];
            string[] columns = new string[dt.Columns.Count];
            string[,] item = new string[dt.Rows.Count, dt.Columns.Count];

           //데이터데이플에있는 값들을 엑셀에담기위해서 item이라는 배열에담기
            if (dt.Rows.Count > 0)
            {
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                   //DataTable 첫 Row에있는 컬럼명을 담기
                    headers[c] = dt.Columns[c].ColumnName;
                    //컬럼 위치값을 가져오기
                    columns[c] = ExcelColumnIndexToName(c);
                }


                for (int rowNo = 0; rowNo < dt.Rows.Count; rowNo++)
                {
                    for (int colNo = 0; colNo < dt.Columns.Count; colNo++)
                    {
                        
                        item[rowNo, colNo] = dt.Rows[rowNo][colNo].ToString();
                    }

                    iRow++;
                }
            }

            //해당위치에 컬럼명을 담기
            workSheet.get_Range("A1", columns[dt.Columns.Count - 1] + "1").Value2 = headers;
           //해당위치부터 데이터정보를 담기
            workSheet.get_Range("A2", columns[dt.Columns.Count - 1] + (dt.Rows.Count + 1).ToString()).Value = item;
            workSheet.Cells.NumberFormat = @"@";
            workSheet.Columns.AutoFit();
            workBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, 
            Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);
            appliCation.Quit();
            releaseObject(appliCation);
            releaseObject(workSheet);
            releaseObject(workBook);

        }
        catch (Exception theException)
        {
            MessageBox.Show(theException.Message.ToString());
        }
}




  //컬럼의 좌표값 잡기
    private string ExcelColumnIndexToName(int Index)
    {
        string range = "";
        if (Index < 0) return range;
        for (int i = 1; Index + i > 0; i = 0)
        {
            range = ((char)(65 + Index % 26)).ToString() + range;
            Index /= 26;
        }
        if (range.Length > 1) range = ((char)((int)range[0] - 1)).ToString() + range.Substring(1);
        return range;
    }


  //메모리해제
   private static void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception e)
        {
            obj = null;
        }
        finally
        {
            GC.Collect();
        }
    }
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @mlddlhk! You received a personal award!

1 Year on Steemit

Click here to view your Board of Honor

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @mlddlhk! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!