C# 导出为二维表头的EXCEL

#include "StdAfx.h"
#include "ExportData.h"
using namespace System::Windows ::Forms ;
using namespace TDLTESYSTEM;
ExportData::ExportData(DataTable^ dt,String^ filename,String^ title)
{
this->dt=dt;
this->filename =filename;
this->title =title;
ExportToExcel(dt,filename,title);
}
int ExportData::ExportToExcel(DataTable^ dt,String^ filename,String^ title)
{
 try
    {
        int sheetRows = 65535;//设置Sheet的行数,此为最大上限,本来是65536,因表头要占去一行
        int sheetCount = (dt->Rows->Count - 1) / sheetRows + 1;//计算Sheet数
  System::GC::Collect();//垃圾回收

  Excel::Application^ excel;
  Excel::Workbook^ xBk;
  Excel::Worksheet^ xSt = nullptr;
  excel = gcnew Excel::ApplicationClass();
  xBk = excel->Workbooks->Add(true);
  //Excel::_Worksheet ^ss;
  
  //ss->GetType();
        //定义循环中要使用的变量
        int dvRowStart;
        int dvRowEnd;
        int rowIndex = 0;
        int colIndex = 0;
        //对全部Sheet进行操作
        for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
        {
            //初始化Sheet中的变量
            rowIndex = 1;
            colIndex = 1;
            //计算起始行
            dvRowStart = sheetIndex * sheetRows;
            dvRowEnd = dvRowStart + sheetRows - 1;
            if (dvRowEnd > dt->Rows->Count - 1)
            {
                dvRowEnd = dt->Rows->Count - 1;
            }
            //创建一个Sheet
            if (nullptr == xSt)
            {
    xSt = (Excel::Worksheet ^)xBk->Worksheets->Add(System::Type::Missing, System::Type::Missing, 1, System::Type::Missing);
            }
            else
            {
    xSt = (Excel::Worksheet^)xBk->Worksheets->Add(System::Type::Missing, xSt, 1, System::Type::Missing);
            }
            //设置Sheet的名称
            xSt->Name = title;
            if (sheetCount > 1)
            {
    xSt->Name += System::Convert ::ToString ((int)(sheetIndex + 1));
            }
            //取得标题
 
    //合并单元格
   System::Object ^ cell1=excel->Cells [rowIndex,colIndex];
   System::Object ^ cell2=excel->Cells [rowIndex,dt->Columns ->Count ];
   Microsoft::Office ::Interop ::Excel ::Range ^ range=(Microsoft::Office ::Interop ::Excel ::Range ^)xSt->Range [cell1,cell2];
   range->MergeCells =true;
   //一级表头
   excel->Cells [rowIndex,colIndex]=title;
   excel->ActiveCell ->Font ->Size =20;
   excel->ActiveCell ->HorizontalAlignment =Microsoft::Office ::Interop ::Excel ::Constants ::xlCenter ;//居中

   //二级表头
   for(int i =0;i<dt->Columns->Count;i++)
   {
                excel->Cells[rowIndex+1, colIndex++] = dt->Columns[i]->ColumnName;
            }
            //取得表格中数据
            int drvIndex;
            for (drvIndex = dvRowStart; drvIndex <= dvRowEnd; drvIndex++)
            {
    DataRow^ row = dt->Rows[drvIndex];
                //新起一行,当前单元格移至行首
                rowIndex++;
                colIndex = 1;
                for(int i =0;i<dt->Columns->Count;i++)
                {
     if (dt->Columns[i]->DataType == System::Type::GetType("System.DateTime"))
                    {
      excel->Cells[rowIndex+1, colIndex] = System::Convert::ToString(row[dt->Columns[i]->ColumnName]);//time2;
                    }
                    else if (dt->Columns[i]->DataType == System::Type::GetType("System.String"))
                    {
                        excel->Cells[rowIndex+1, colIndex] = row[dt->Columns[i]->ColumnName]->ToString();
                    }
                    else
                    {
                        excel->Cells[rowIndex+1, colIndex] = row[dt->Columns[i]->ColumnName]->ToString();
                    }
                    colIndex++;
                }
            }
        xBk->SaveCopyAs(filename);
        xBk->Close(false, nullptr, nullptr);
        excel->Quit();

  System::Runtime::InteropServices::Marshal::ReleaseComObject(xBk);
        System::Runtime::InteropServices::Marshal::ReleaseComObject(excel);
        System::Runtime::InteropServices::Marshal::ReleaseComObject(xSt);

        xBk = nullptr;
        excel = nullptr;
        xSt = nullptr;
  System::GC::Collect();
        //返回写入服务器Excel文件的路径
        return 1;
    }
    catch(Exception^ ex)
    {
  MessageBox::Show(ex->ToString());
        return -1;
    }
   
}

转载于:https://www.cnblogs.com/fmlblog/p/3188095.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Connection.getConnection(); String sql = "DELETE FROM car WHERE id = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1,你可以使用Microsoft.Office.Interop.Excel命名空间下的类来实现将C#二维数组数据导出Excel的 selectedCar.getId()); stmt.executeUpdate(); loadCarData(); } catch (Exception e) { e.printStackTrace(); } finally功能。以下是一个简单的代码示例: ```csharp using Excel = Microsoft.Office.Interop.Excel; // 创建Excel应用 { DatabaseConnection.close(conn, stmt, null); } } } private void showRentCarDialog() { Car程序对象 Excel.Application excelApp = new Excel.Application(); // 创建一个新的工作簿 Excel.Workbook workbook = excelApp.Work selectedCar = carTable.getSelectionModel().getSelectedItem(); if (selectedCar != null && !selectedCar.isStatus()) { Dialog<Orderbooks.Add(); // 创建一个新的工作表 Excel.Worksheet worksheet = workbook.Sheets.Add(); // 获取二维数组的行> dialog = new Dialog<>(); dialog.setTitle("租车"); dialog.setHeaderText(null); // 创建对话框控件 Label nameLabel = new Label("姓名:"); TextField nameField = new TextField(); Label startTimeLabel = new Label("开始时间数和列数 int rows = array.GetLength(0); int columns = array.GetLength(1); // 将数据写入Excel单元:"); DatePicker startTimePicker = new DatePicker(); Label endTimeLabel = new Label("结束时间:"); DatePicker endTimePicker = new格 for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { worksheet.Cells[i + 1, j + 1] = array[i, j]; } } // DatePicker(); // 添加对话框控件到布局中 GridPane gridPane = new GridPane(); gridPane保存工作簿并关闭Excel应用程序 workbook.SaveAs("output.xlsx"); workbook.Close(); excelApp.Quit(); ``` .setHgap(10); gridPane.setVgap(10); gridPane.setPadding(new Insets(10, 10, 在上面的代码中,`array`是你要导出的二维数组,`output.xlsx`是导出Excel10, 10)); gridPane.add(nameLabel, 0, 0); gridPane.add(nameField, 1, 0); gridPane.add(startTimeLabel, 0, 1); gridPane.add(startTimePicker, 1, 1); 文件名。你需要在项目中添加对`Microsoft.Office.Interop.Excel`的引用,以便使用该命名空间下的类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值