| bool WriteExcelFile_OfficeInterop(String^ path, DataSet^ dt, |
| const std::vector<std::string>& sheetName, bool hideColumnName) |
| { |
| |
| if (System::IO::File::Exists(path)) |
| { |
| try |
| { |
| System::IO::File::Delete(path); |
| } |
| catch (System::Exception^ e) |
| { |
| System::Windows::Forms::MessageBox::Show("Delete Excel File Failed, Please Check File Statue.", |
| "Delete File Error", |
| System::Windows::Forms::MessageBoxButtons::OK, |
| System::Windows::Forms::MessageBoxIcon::Error); |
| return false; |
| } |
| } |
| |
| Excel::Application^ excelApp = nullptr; |
| Excel::_Workbook^ workbook = nullptr; |
| Excel::Worksheet^ worksheet = nullptr; |
| Excel::Range^ range = nullptr; |
| Excel::Range^ firstRow = nullptr; |
| Excel::Sort^ sort = nullptr; |
| bool ret = true; |
| try |
| { |
| |
| excelApp = gcnew Excel::ApplicationClass(); |
| |
| workbook = excelApp->Workbooks->Add(true); |
| |
| for (int i = 0; i < dt->Tables->Count; ++i) |
| { |
| |
| worksheet = (Excel::Worksheet^)workbook->Sheets->Add(Type::Missing, workbook->Sheets[workbook->Worksheets->Count], Type::Missing, Excel::XlSheetType::xlWorksheet); |
| |
| worksheet->Name = StringToSystemString(sheetName[i]); |
| |
| |
| int headerRowIndex = 0; |
| if (hideColumnName == false) |
| { |
| |
| headerRowIndex = 1; |
| |
| for (int j = 0; j < dt->Tables[i]->Columns->Count; ++j) |
| { |
| worksheet->Cells[1, j + 1] = dt->Tables[i]->Columns[j]->ColumnName; |
| } |
| } |
| |
| |
| int numCols = dt->Tables[i]->Columns->Count; |
| int numRows = dt->Tables[i]->Rows->Count; |
| range = worksheet->Range[worksheet->Cells[headerRowIndex + 1, 1], |
| worksheet->Cells[headerRowIndex + numRows, numCols]]; |
| |
| |
| array<System::Object^, 2>^ dataArray = gcnew array<System::Object^, 2>(numRows, numCols); |
| for (int row = 0; row < numRows; ++row) |
| { |
| for (int col = 0; col < numCols; ++col) |
| { |
| dataArray[row, col] = dt->Tables[i]->Rows->default[row]->default[col]; |
| } |
| } |
| range->Value2 = dataArray; |
| |
| |
| worksheet->UsedRange->Columns->AutoFit(); |
| } |
| |
| safe_cast<Excel::Worksheet^>(workbook->Sheets["Sheet1"])->Delete(); |
| |
| safe_cast<Excel::_Worksheet^>(workbook->Sheets[1])->Activate(); |
| |
| workbook->SaveAs(path, Type::Missing, Type::Missing, |
| Type::Missing, Type::Missing, Type::Missing, Microsoft::Office::Interop::Excel::XlSaveAsAccessMode::xlNoChange, |
| Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing); |
| |
| |
| workbook->Close(Type::Missing, Type::Missing, Type::Missing); |
| |
| |
| excelApp->Quit(); |
| } |
| catch (System::Exception^ e) |
| { |
| System::Windows::Forms::MessageBox::Show(e->Message, |
| "Write File Error", |
| System::Windows::Forms::MessageBoxButtons::OK, |
| System::Windows::Forms::MessageBoxIcon::Error); |
| |
| |
| if (workbook != nullptr) |
| { |
| workbook->Close(Type::Missing, Type::Missing, Type::Missing); |
| } |
| if (excelApp != nullptr) |
| { |
| excelApp->Quit(); |
| } |
| ret = false; |
| } |
| finally |
| { |
| |
| if (sort != nullptr) |
| { |
| Marshal::ReleaseComObject(sort); |
| } |
| if (firstRow != nullptr) |
| { |
| Marshal::ReleaseComObject(firstRow); |
| } |
| if (range != nullptr) |
| { |
| Marshal::ReleaseComObject(range); |
| } |
| |
| if (worksheet != nullptr) |
| { |
| Marshal::ReleaseComObject(worksheet); |
| } |
| if (workbook != nullptr) |
| { |
| Marshal::ReleaseComObject(workbook); |
| } |
| if (excelApp != nullptr) |
| { |
| Marshal::ReleaseComObject(excelApp); |
| } |
| } |
| return ret; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库