1.首先需要一点文件:
#include "BasicExcel.h"
using namespace YExcel;
2.下面这种方式创建的excel都不可以在windows下打开,存放过数据的也不行
BasicExcel e;
// Load a workbook with one sheet, display its contents and
// save into another file.
e.Load("E:\\vs2017_project\\MFC_EXCEL\\example.xlsx");//加载工作簿
BasicExcelWorksheet* sheet1 = e.GetWorksheet("Sheet1");//获取工作表指针
BasicExcelCell* cell;
if (sheet1)
{
size_t maxRows = sheet1->GetTotalRows();//获取工作表总行数
size_t maxCols = sheet1->GetTotalCols();//总列数
cout << "Dimension of " << sheet1->GetAnsiSheetName() <<
" (" << maxRows << ", " << maxCols << ")" << endl;
printf(" ");
for (size_t c = 0; c < maxCols; ++c) printf("%10d", c + 1);
cout << endl;
for (size_t r = 0; r < maxRows; ++r)
{
printf("%10d", r + 1);
for (size_t c = 0; c < maxCols; ++c)
{
BasicExcelCell* cell = sheet1->Cell(r, c);//返回指向单元格的指针
switch (cell->Type())//返回值的类型
{
case BasicExcelCell::UNDEFINED:
printf(" ");
break;
case BasicExcelCell::INT:
printf("%10d", cell->GetInteger());
break;
case BasicExcelCell::DOUBLE:
printf("%10.6lf", cell->GetDouble());
break;
case BasicExcelCell::STRING:
printf("%10s", cell->GetString());
break;
case BasicExcelCell::WSTRING:
wprintf(L"%10s", cell->GetWString());
break;
}
}
cout << endl;
}
cell = sheet1->Cell(1, 3);//得到单元格指针
cell->SetDouble(3.141592654);//存入数据
}
cout << endl;
e.Save();//保存
e.SaveAs("E:\\vs2017_project\\MFC_EXCEL\\example2.xlsx");//文件另存为
// Create a new workbook with 2 worksheets and write some contents.
e.New(2);//创建新表
e.RenameWorksheet("Sheet1", "Test1");//重命名表格
BasicExcelWorksheet* sheet = e.GetWorksheet("Test1");
//memcpy(s2, sheet->Cell(1, 2), i); //读取单元格并复制到s2的方式
//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);
}
e.SaveAs("E:\\vs2017_project\\MFC_EXCEL\\example3.xlsx");
// Load the newly created sheet and display its contents
e.Load("E:\\vs2017_project\\MFC_EXCEL\\example3.xlsx");
size_t maxSheets = e.GetTotalWorkSheets();
cout << "Total number of worksheets: " << e.GetTotalWorkSheets() << endl;
for (size_t i = 0; i < maxSheets; ++i)
{
BasicExcelWorksheet* sheet = e.GetWorksheet(i);
if (sheet)
{
size_t maxRows = sheet->GetTotalRows();//行数
size_t maxCols = sheet->GetTotalCols();//列数
cout << "Dimension of " << sheet->GetAnsiSheetName() <<
" (" << maxRows << ", " << maxCols << ")" << endl;
if (maxRows > 0)
{
printf(" ");
for (size_t c = 0; c < maxCols; ++c) printf("%10d", c + 1);
cout << endl;
}
for (size_t r = 0; r < maxRows; ++r)
{
printf("%10d", r + 1);
for (size_t c = 0; c < maxCols; ++c)
{
cout << setw(10) << *(sheet->Cell(r, c));//输出
// Another way of printing a cell content.
}
cout << endl;
}
//if (i == 0)
//{
// ofstream f("example4.csv");
// sheet->Print(f, ',', '\"'); // Save the first sheet as a CSV file.
// f.close();
//}
}
cout << endl;
}
以上来自:https://blog.csdn.net/greless/article/details/76922707?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
其中有非常详细的BasicExcel详细的函数介绍。