zoukankan      html  css  js  c++  java
  • 反射导出excel案例

    1.代码案例:

    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
    SetSearchValue();
    Dictionary<string, string> titles = new Dictionary<string, string>();
    titles.Add("ProductName", "产品名称");

    titles.Add("DanOrShuang", "单机/双机");

    titles.Add("SN1", "SN1");

    titles.Add("SN2", "SN2");

    titles.Add("InstallQTY", " 安装数量");

    titles.Add("ServerName", "服务器名称");

    titles.Add("Area", " 区域");

    titles.Add("CustomerName", " 客户名称");

    titles.Add("CinemaName", "影院名称");

    SII_ExcelExportUtil.ExcelExport("Tenancy Equipment", "Tenancy Equipment", this, _bll.ExportExcel(_search), titles);
    }

    ======================
    public class SII_ExcelExportUtil:ExcelExportUtil
    {
    public static void ExcelExport(string FileName, string sheetName, Page page, IList list,Dictionary<string,string> Titles)
    {

    new SII_ExcelExportUtil().ExcelExportData(FileName, sheetName, page, list, Titles);
    }
    }

    2.

    ----------------

    public virtual bool ExcelExportData(string FileName, string sheetName, Page page, IList list, Dictionary<string, string> Titles)
    {
    Excel.Application m_xlApp = null;
    string path;
    string floder;
    GetPath(FileName, page, out path, out floder);
    bool result = false;
    //if (list != null && list.Count > 0)
    if (Titles!=null && Titles.Count>0)
    {

    int rowNum = list.Count;//行数
    int columnNum = Titles.Count;//列数
    m_xlApp = new Excel.Application();
    m_xlApp.DisplayAlerts = false;//不显示更改提示
    m_xlApp.Visible = false;

    Excel.Workbooks workbooks = m_xlApp.Workbooks;
    Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1

    try
    {
    worksheet.Name = sheetName;
    string[,] datas = new string[rowNum + 1, columnNum];

    int columnIndex = 0;
    foreach (KeyValuePair<string, string> item in Titles)
    {
    datas[0, columnIndex] = item.Value;
    columnIndex++;
    }
    Excel.Range range = SetColumsStyle(columnNum, worksheet);

    if (list != null && list.Count > 0)
    {
    PropertyInfo[] Columns = list[0].GetType().GetProperties();
    for (int index = 0; index < list.Count; index++)
    {

    //在当前行中,逐列获得数据
    int cIndex = 0;
    foreach (KeyValuePair<string, string> item in Titles)
    {
    PropertyInfo pro = Columns.First(p => p.Name == item.Key);
    string value = "";
    if (pro != null)
    {
    object resultValue = pro.GetValue(list[index], null);
    if (resultValue is DateTime)
    {
    value = Convert.ToDateTime(resultValue).ToString("yyyy/MM/dd");
    }
    else value = resultValue == null ? "" : resultValue.ToString();
    }
    datas[index + 1, cIndex] = value;
    cIndex++;
    }
    }
    }
    range = SetRowData(m_xlApp, path, rowNum, columnNum, worksheet, datas, range);
    workbook.SaveAs(path);
    result = true;
    }
    catch (Exception ex)
    {
    }
    finally
    {
    EndReport(m_xlApp);
    }
    HttpDown(path, page, floder);
    }
    return result;
    }

    -----------

    private Excel.Range SetRowData(Excel.Application m_xlApp, string path, int rowNum, int columnNum, Excel.Worksheet worksheet, string[,] datas, Excel.Range range)
    {
    Excel.Range fchR = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]];
    fchR.Value2 = datas;

    worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
    m_xlApp.WindowState = Excel.XlWindowState.xlMinimized;

    range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]];
    //range.Interior.ColorIndex = 15;//15代表灰色
    range.Font.Size = 9;
    range.RowHeight = 14.25;
    //range.Borders.LineStyle = 1;
    range.HorizontalAlignment = 1;
    ActiveWindow(m_xlApp);
    //workbook.SaveCopyAs(path);

    return range;
    }

  • 相关阅读:
    结对编程2—单元测试
    个人作业2—英语学习APP案例分析
    结对作业--基于GUI的四则运算生成器
    基于控制台的四则运算
    关于软件工程这门课
    个人作业3——个人总结(Alpha阶段)
    结对编程2——单元测试
    个人作业2——英语学习APP案例分析
    结对作业--基于GUI的四则运算生成器
    个人作业一
  • 原文地址:https://www.cnblogs.com/guozefeng/p/3584081.html
Copyright © 2011-2022 走看看