libxl 的使用,讀取時間格式


最近開發使用到 libxl,用的是3.8.0 破解版。

具體過程:

1、將lib.dll放在exe同目錄下,在代碼中引用 libxl.lib

  #pragma comment(lib, ".\\Lib\\libxl.lib")

2、包含頭文件 libxl.h

3、實例

//m_strFilePath為excel文件的完整路徑
CString ext = ::PathFindExtension(m_strFilePath);
	if(ext.CompareNoCase(L".xlsx") == 0)
		book = xlCreateXMLBook();  //針對.xlsx
	else
		book = xlCreateBook();  //針對.xls

	if(!book)
	{
		return;
	}

	const wchar_t * x = L"Halil Kural";
	const wchar_t * y = L"windows-2723210a07c4e90162b26966a8jcdboe";
	book->setKey(x, y);  //設置key,即破解^_^

	if(!book->load(m_strFilePath))
	{
		return;
	}

	int sheetCount = book->sheetCount(); //工作表總數量
	for (int index = 0; index < sheetCount; ++index)
	{
		Sheet *sheet =book->getSheet(index++);
		if(!sheet)
			continue;
		int firstRow = sheet->firstRow(); //有數據的第一行行號
		int lastRow = sheet->lastRow();  //有數據的最后一行行號
		int firstCol = sheet->firstCol(); //有數據的第一列列號
		int lastCol = sheet->lastCol();   //有數據的最后一列列號

		//找出表頭(測試數據)
		map<int,wstring> mapColNames;
		for(int c = firstCol; c < lastCol; ++c)
		{
			const wchar_t* str = sheet->readStr(firstRow, c);
			if(!str)
				continue;
			mapColNames[c] = str;
		}

		//確定每個表頭代表的意義(測試數據)
		map<int,eFIELD> mapColTypes;
		for (auto it = mapColNames.begin(); it != mapColNames.end(); ++it)
		{
			eFIELD e = GetFieldTypeByName(it->second.c_str());
			mapColTypes[it->first] = e;
		}

		//逐行讀取數據
		for (int row = firstRow+1; row < lastRow; ++row)
		{
			for(int c = firstCol; c < lastCol; ++c)
			{
                                        CString strValue;
                                        CellType t = sheet->cellType(row, c);
					if(t == CELLTYPE_NUMBER)
					{
						double db = sheet->readNum(row, c);//test
						LONG64 number = (LONG64)db;
						if(number > 0)
							strValue.Format(L"%I64d", number);
					}
                                        else
                                             const wchar_t* s = sheet->readStr(row, c); //讀取內容 
}          } }

  

4、讀取時間格式的數據

 1 CString strValue;
 2                 bool bDate = sheet->isDate(row, col);
 3                 if (bDate)
 4                 {
 5                     double db = sheet->readNum(row, col);
 6                     int year=0, month=0, day=0;
 7                     bool b = book->dateUnpack(db, &year, &month, &day);
 8 
 9     //將讀取的時間 轉換為時間戳
10                     unsigned __int64 ftNow;
11                     SYSTEMTIME time;
12                     ZeroMemory(&time, sizeof(SYSTEMTIME)),
13                     time.wYear = year;
14                     time.wMonth = month;
15                     time.wDay = day;
16                     SystemTimeToFileTime(&time,(LPFILETIME)&ftNow);
17                     __int64 timeStamp =  (__int64)((ftNow-0x019db1ded53e8000)/10000);//毫秒
18                     strValue.Format(L"%I64d", timeStamp);
19                 }    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM