DataTable leads to Excel

Source: Internet
Author: User

/// <summary>/// 将DataTalbe导出到Excel中/// </summary>/// <param name="dt"></param>/// <param name="ProjectName">生成的Excel的Sheet的名字</param>/// <param name="filePath">保存的路径</param>publicstaticvoidExport(System.Data.DataTable dt, stringfilePath){    if(dt == null)    {        thrownewException("数据表中无数据");    }    inteRowIndex = 1;    inteColIndex = 1;    intcols = dt.Columns.Count;    introws = dt.Rows.Count;    Microsoft.Office.Interop.Excel.Application xlApp = newMicrosoft.Office.Interop.Excel.ApplicationClass();    Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);    try    {        //列名的处理        for(inti = 0; i < cols; i++)        {            xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;            eColIndex++;        }        //列名加粗显示        xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Bold = true;        xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Name = "Arial";        xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Size = "10";        eRowIndex++;        for(inti = 0; i < rows; i++)        {            eColIndex = 1;            for(intj = 0; j < cols; j++)            {                xlApp.Cells[eRowIndex, eColIndex] = dt.Rows[i][j].ToString();                eColIndex++;            }            eRowIndex++;        }        //控制单元格中的内容。        xlApp.Cells.EntireColumn.AutoFit();        xlApp.DisplayAlerts = false;        xlBook.SaveCopyAs(filePath);        xlApp.Workbooks.Close();    }    catch    {        throw;    }    finally    {        xlApp.Quit();        //杀掉Excel进程。        GC.Collect();    }}

Note: Using Microsoft.Office.Interop.Excel; VS2010 under Add Reference COM option, look for Microsoft Office 12.0 Object Library in Microsoft Excel 14.0 Object Library

Microsoft.Office.Core Microsoft.Office.Interop.Excel appears in the reference directory

If Excel.applicationclass () cannot be nested in code, use the applicable interface instead

Change the reference Microsoft.Office.Interop.Excel right-click property into the interop type to False

DataTable leads to Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.