• Datatable导出到Excel


    /// <summary>

    ///
    /// </summary>
    /// <param name="dt"></param>
    protected void ExportExcel(DataTable dt)
    {
        if (dt == null||dt.Rows.Count==0) return;
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
     
        if (xlApp == null)
        {
            return;
        }
        System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
        Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
        Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
        Microsoft.Office.Interop.Excel.Range range;
        long totalCount = dt.Rows.Count;
        long rowRead = 0;
        float percent = 0;
        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;
        }
        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].ToString();
            }
            rowRead++;
            percent = ((float)(100 * rowRead)) / totalCount;
        }
        xlApp.Visible = true;
    }
     
     
    EXCEL中插入图片
    xlApp.Visible = true;之前插入以下代码
    worksheet.Shapes.AddPicture("C:\Users\spring\Desktop\1.gif", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 200, 200, 300);
        worksheet.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "123456", "Red", 15, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 150, 200);
  • 相关阅读:
    MySQL数据透视功能
    MySQL 姓名两个字中间加两个空格
    Back To Basics: Clustered vs NonClustered Indexes; what’s the difference?
    炉石传说 佣兵战纪 贫瘠之地
    When to Use Clustered or NonClustered Indexes in SQL Server
    Stairway to SQL Server Indexes
    Why use the INCLUDE clause when creating an index?
    Clustered vs Nonclustered Index: Key Differences with Example
    SQL Server: Order of Rows
    Being Agile vs. doing Agile: What's the difference?
  • 原文地址:https://www.cnblogs.com/nuoyi/p/13492773.html
Copyright © 2020-2023  润新知