我在asp.net应用程序中执行导出到Excel过程。
在该过程中,我使用以下代码将DateTime值写入excel:
string reqDate = item.requestedDate.ToString("MM/dd/yyyy hh:mm:ss tt");
Response.Write(reqDate + "\t");
但excel文件中的结果错过了日期时间中的 second 部分,如下所示:
4/3/2014 17:05
我希望它看起来像 04/03/2014 05:05:19 PM
怎么做?
请帮忙。
答案 0 :(得分:2)
看起来您要导出为CSV而不是Excel。 Excel读取CSV,识别DateTime,并使用其默认的特定于文化的格式对其进行格式化。
如果您想强制将其格式化为文字,请查看this question的答案。
或者,您可以考虑使用EPPlus或Aspose Cells等工具生成正版Excel文件,而不是CSV。
答案 1 :(得分:2)
此问题是由Excel在CSV文档中解析DateTime的原因造成的。更具体地说,如果DateTime的#include <iostream>
#include <string>
#include <fstream>
class CardSearch
{
protected:
std::ifstream cardNumbers;
public:
CardSearch(std::string fileName)
{
cardNumbers.open(fileName, std::ios::in);
if (!cardNumbers.is_open())
{
std::cout << "Unable to open: " << fileName;
}
return;
}
std::string Find(std::string lastName, std::string firstName)
{
// Creating string variables to hold first and last name
// as well as card number. Also creating bools to decide whether
// or not the person has been found or if the last name is the only
// identifier for a found person
std::string lN;
std::string fN;
std::string creditNumber;
bool foundPerson = false;
// By using the seekg and tellg functions, we can find our place
// in the file and also calculate the amount of lines within the file
cardNumbers.seekg(0, std::ios::beg);
cardNumbers.clear();
std::streamsize first = cardNumbers.tellg();
cardNumbers.ignore(std::numeric_limits<std::streamsize>::max());
cardNumbers.clear();
std::streamsize last = cardNumbers.tellg();
cardNumbers.seekg(0, std::ios::beg);
std::streamsize lineNumbers = (last / 57);
std::streamsize middle;
while (first <= lineNumbers)
{
middle = (first + lineNumbers) / 2;
// middle * 57 takes us to the beginning of the correct line
cardNumbers.seekg(middle * 57, std::ios::beg);
cardNumbers.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cardNumbers >> lN >> fN;
if (lN < lastName)
{
first = middle + 1;
}
else if (lN > lastName)
{
lineNumbers = middle - 1;
}
else
{
if (fN < firstName)
{
first = middle + 1;
}
else if (fN > firstName)
{
lineNumbers = middle - 1;
}
else if (fN == firstName)
{
foundPerson = true;
break;
}
}
}
if (foundPerson)
{
// When a person is found, we seek to the correct line position and
// offset by another 40 characters to receive the card number
cardNumbers.seekg((middle * 57) + 40, std::ios::beg);
std::cout << lN << ", " << fN << " ";
cardNumbers >> creditNumber;
return creditNumber;
}
return "Unable to find person.\n";
}
};
int main()
{
CardSearch CS("C:/Users/Rafael/Desktop/StolenNumbers.txt");
std::string S = CS.Find("Ampersand", "Gregorina");
std::cout << S;
std::cin.ignore();
std::cin.get();
return 0;
}
属性小于或等于12。
在您的示例(04/03/2014)Day
中,Excel以您不想要的方式对其进行格式化。但是对于Day <= 12
的日期时间(例如2014年4月23日),Excel会按照您的预期格式化。
让Excel在Excel中显示所需的技巧是将它们包装在Excel公式中:Day > 12
="04/03/2014 05:05:19 PM"