zoukankan      html  css  js  c++  java
  • Excel:合并某一列

    //创建一个Excel.Application的新进程
    Microsoft.Office.Interop.Excel.Application app = new Application();
    if (app == null) return;
    app.Visible = false;
    app.UserControl = true;
    Workbooks workbooks = app.Workbooks;
    _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);//这里的Add方法里的参数就相当于继承了一个空模板(暂这样理解吧)
    Sheets sheets = workbook.Worksheets;
    _Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
    if (worksheet == null) return;

    DataSet ds = Convert_StringToDataSet(str);
    System.Data.DataTable dt = ds.Tables[0];
    string bidName = "";
    int projNum = 0;
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        projNum++;
        bidName = dt.Rows[i]["BigFactorClassName"].ToString();
        worksheet.Cells[1 + i, 1] = i + 1;
        worksheet.Cells[1 + i, 3] = dt.Rows[i]["SmallFactorClassName"];
        worksheet.Cells[1 + i, 4] = dt.Rows[i]["FactorClassName"];
        if (i == 0)//如果是第一行,就直接写了
        {
            worksheet.Cells[1 + i, 2] = dt.Rows[i]["BigFactorClassName"];
        }
        else if (bidName == dt.Rows[i - 1]["BigFactorClassName"].ToString())//此行与上一行时行比较,如果相等就不写值
        {
            worksheet.get_Range(worksheet.Cells[i + 1, 2], worksheet.Cells[i, 2]).Merge(Type.Missing);
        }
        else
        {
            worksheet.Cells[1 + i, 2] = dt.Rows[i]["BigFactorClassName"];
        }
    }

    worksheet.Name = "导出Excel测试1";
    string tick = DateTime.Now.Ticks.ToString();
    string save_path = "C:\\Users\\Administrator\\Desktop\\" + tick + ".xls";

    workbook.SaveAs(save_path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    Response.Write("<script>alert('导出成功!')</script>");

  • 相关阅读:
    结构体比较
    不定长参数列表用法
    接口
    字符串数据类型
    *和**的打包和解包
    python类常用装饰器
    继承的实现
    map用法
    包的导入和init函数
    协程
  • 原文地址:https://www.cnblogs.com/pnljs/p/2363132.html
Copyright © 2011-2022 走看看