zoukankan      html  css  js  c++  java
  • asp.net C# 导出EXCEL数据

    if (dt == null)
    {
    return "";
    }
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    if (xlApp == null)
    {
    return "无法创建Excel对象,可能您的机子未安装Excel";
    }
    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];//取得Sheet1
    Microsoft.Office.Interop.Excel.Range range;
    long totalCount = dt.Rows.Count;

    long rowRead = 0;
    float percent = 0;

    string[] ColumnList = columns.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
    //写入字段
    for (int i = 0; i < ColumnList.Length; i++)
    {
    worksheet.Cells[1, i + 1] = ColumnList[i];
    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];
    }
    rowRead++;
    percent = ((float)(100 * rowRead)) / totalCount;
    }
    xlApp.Visible = true;

  • 相关阅读:
    eclipse恢复界面默认设置
    文件夹的拷贝
    文件的输入输出
    十进制转二进制,八进制,十六进制(查表法)
    数组元素的查找(折半查找)
    C++几个小函数
    C++ 内部排序(一)
    C++实现链表
    C++输出IP地址段内的合法地址
    我看软件工程师的职业规划
  • 原文地址:https://www.cnblogs.com/wfpanskxin/p/4464037.html
Copyright © 2011-2022 走看看