zoukankan      html  css  js  c++  java
  • 导出Excel

    引用 NPOI.dll

    public void WriteDataToExcel(ParaInfo tParallelism)
    {
    int isHaveFile = 0;
    string DirName = "Excel";
    if (Directory.Exists(DirName) == false)
    { //判断该目录是否存在,不存在则创建
    Directory.CreateDirectory(DirName);
    }

    for (int n = 0; n < 2; n++)
    {
    string path = System.IO.Directory.GetCurrentDirectory();
    string FileName = "\Excel", time;

    if (n == 0)
    {
    time = "总表";
    }
    else
    {
    time = DateTime.Now.ToString("yyyyMMdd");
    }

    string FileTail = ".xls";
    char File1 = '\';
    string DestPath = path + File1 + DirName + FileName + time + FileTail;

    if (File.Exists(DestPath))
    {
    isHaveFile = 0;
    }
    else
    {
    isHaveFile = 1;
    }

    if (isHaveFile == 1)
    {
    HSSFWorkbook workbook2003 = new HSSFWorkbook(); //新建工作簿

    #region 注册表

    workbook2003.CreateSheet("注册表"); //新建1个Sheet工作表
    HSSFSheet SheetOne = (HSSFSheet)workbook2003.GetSheet("注册表"); //获取名称为Sheet1的工作表
    //对每一行创建10个单元格
    SheetOne.CreateRow(0); //创建首行

    HSSFRow SheetRow = (HSSFRow)SheetOne.GetRow(0); //获取Sheet1工作表的首行
    HSSFCell[] SheetCell = new HSSFCell[10];

    for (int i = 0; i < 8; i++)
    {
    SheetCell[i] = (HSSFCell)SheetRow.CreateCell(i); //为第一行创建10个单元格
    }
    SheetOne.SetColumnWidth(0, 20 * 256);
    SheetOne.SetColumnWidth(1, 20 * 256);
    SheetOne.SetColumnWidth(2, 20 * 256);
    SheetOne.SetColumnWidth(3, 20 * 276);
    SheetOne.SetColumnWidth(4, 20 * 276);
    SheetOne.SetColumnWidth(5, 20 * 256);
    SheetOne.SetColumnWidth(6, 20 * 256);
    SheetOne.SetColumnWidth(7, 20 * 256);
    //创建之后就可以赋值了
    SheetCell[0].SetCellValue("序号");
    SheetCell[1].SetCellValue("aa");
    SheetCell[2].SetCellValue("bb");
    SheetCell[3].SetCellValue("ID");
    SheetCell[4].SetCellValue("地址");
    SheetCell[5].SetCellValue("日期");
    SheetCell[6].SetCellValue("备注");

    #endregion 注册表

    FileStream file2003 = new FileStream(DestPath, FileMode.OpenOrCreate);
    workbook2003.Write(file2003);
    file2003.Close();
    workbook2003.Close();
    }

    {
    FileStream fs = new FileStream(DestPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
    HSSFWorkbook workbook2003 = new HSSFWorkbook(fs);

    #region 注册表

    HSSFSheet SheetOne = (HSSFSheet)workbook2003.GetSheet("注册表"); //获取名称为Sheet1的工作表

    int LastNULLRow = 0;
    for (LastNULLRow = 0; LastNULLRow < 65535; LastNULLRow++)
    {
    IRow row = (HSSFRow)SheetOne.GetRow(LastNULLRow);
    if (row == null) break;
    }
    HSSFRow SheetRow = (HSSFRow)SheetOne.GetRow(LastNULLRow); //获取Sheet1工作表的LastNULLRow行

    SheetOne.CreateRow(LastNULLRow);
    SheetRow = (HSSFRow)SheetOne.GetRow(LastNULLRow); //获取Sheet1工作表的首行
    HSSFCell[] SheetCell = new HSSFCell[10];
    for (int i = 0; i < 10; i++)
    {
    SheetCell[i] = (HSSFCell)SheetRow.CreateCell(i); //为第一行创建10个单元格
    }

    SheetCell[0].SetCellValue(LastNULLRow);
    SheetCell[1].SetCellValue(tParallelism.aa); //aa
    SheetCell[2].SetCellValue(tParallelism.bb); //bb
    SheetCell[3].SetCellValue(tParallelism.ID); //ID
    SheetCell[4].SetCellValue(tParallelism.Addr); //地址
    SheetCell[5].SetCellValue(DateTime.Now.ToString()); //时间

    #endregion 注册表

    FileStream file2003 = new FileStream(DestPath, FileMode.OpenOrCreate);
    workbook2003.Write(file2003);
    file2003.Close();
    workbook2003.Close();
    }
    }
    }

  • 相关阅读:
    linux下好用的软件
    linux下常见目录的介绍
    slam相关链接
    GPS 使用的三种坐标系 ECEF、ENU、LLA坐标系
    平面度计算 C++实现
    QT Cmake添加srv和msg
    pcl点云与ros之间的数据类型转换
    ROS分布式设置
    qt中播放视频和音频
    linux tips
  • 原文地址:https://www.cnblogs.com/workstation-nigoudongma/p/9233780.html
Copyright © 2011-2022 走看看