Windows下读取Excel(仅支持xls格式)

读取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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值