#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 ;//居中ui
//二級表頭
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();this
System::Runtime::InteropServices::Marshal::ReleaseComObject(xBk);
System::Runtime::InteropServices::Marshal::ReleaseComObject(excel);
System::Runtime::InteropServices::Marshal::ReleaseComObject(xSt);spa
xBk = nullptr;
excel = nullptr;
xSt = nullptr;
System::GC::Collect();
//返回寫入服務器Excel文件的路徑
return 1;
}
catch(Exception^ ex)
{
MessageBox::Show(ex->ToString());
return -1;
}
}excel