读取excel
int main()
{
YExcel::BasicExcel e;
e.Load(excelfile);
e.Load("D:\\360安全浏览器下载\\info1601434112318.xls");
YExcel::BasicExcelWorksheet* sheet1 = e.GetWorksheet((size_t)0);
if (sheet1)
{
size_t maxRows = sheet1->GetTotalRows();
size_t maxCols = sheet1->GetTotalCols();
cout << "Dimension of " << sheet1->GetAnsiSheetName() << " (" << maxRows << ", " << maxCols << ")" << endl;
for (size_t r = 0; r<maxRows; ++r)
{
for (size_t c = 0; c<maxCols; ++c)
{
std::string strtmp = "";
std::wstring wstrtmp = L"";
wostringstream oss;
printf(" ");
YExcel::BasicExcelCell* cell = sheet1->Cell(r, c);
switch (cell->Type())
{
case YExcel::BasicExcelCell::UNDEFINED:
printf(" ");
break;
case YExcel::BasicExcelCell::INT:
oss << cell->GetInteger() << ".";
wstrtmp = oss.str();
onvalue.push_back(wstrtmp);
//printf("%d", cell->GetInteger());
break;
case YExcel::BasicExcelCell::DOUBLE:
oss << cell->GetDouble() << ".";
wstrtmp = oss.str();
onvalue.push_back(wstrtmp);
//printf("%lf", cell->GetDouble());
break;
case YExcel::BasicExcelCell::STRING:
strtmp = cell->GetString();
wstrtmp = CCodeChange::GetInst().StringToWstring(strtmp);
onvalue.push_back(wstrtmp);
//printf("%s", cell->GetString());
break;
case YExcel::BasicExcelCell::WSTRING:
wstrtmp = cell->GetWString();
onvalue.push_back(wstrtmp);
//wprintf(L"%s", cell->GetWString());
break;
}
}
value.push_back(onvalue);
cout << endl;
}
}
}
修改Excel
e.SaveAs("example2.xls");
e.New(1)
// Create a new workbook with 2 worksheets and write some contents.
BasicExcelWorksheet* sheet = e.GetWorksheet((size_t)0);
BasicExcelCell* cell;
if (sheet)
{
for (size_t c=0; c<4; ++c)
{
cell = sheet->Cell(0,c);
cell->Set((int)c);
}
cell = sheet->Cell(1,3);
cell->SetDouble(3.141592654);
sheet->Cell(1,4)->SetString("Test str1");
sheet->Cell(2,0)->SetString("Test str2");
sheet->Cell(2,5)->SetString("Test str1");
sheet->Cell(4,0)->SetDouble(1.1);
sheet->Cell(4,1)->SetDouble(2.2);
sheet->Cell(4,2)->SetDouble(3.3);
sheet->Cell(4,3)->SetDouble(4.4);
sheet->Cell(4,4)->SetDouble(5.5);
sheet->Cell(4,4)->EraseContents();
}
sheet = e.AddWorksheet("Test2", 1);
sheet = e.GetWorksheet(1);
if (sheet)
{
sheet->Cell(1,1)->SetDouble(1.1);
sheet->Cell(2,2)->SetDouble(2.2);
sheet->Cell(3,3)->SetDouble(3.3);
sheet->Cell(4,4)->SetDouble(4.4);
sheet->Cell(70,2)->SetDouble(5.5);
}
由于篇幅限制将库单独放到上传资源了:https://download.csdn.net/download/gongxie0235/12926766