C# 从Excel读取列,重新格式化单元格
我目前正在尝试从excel电子表格中读取单元格,当我不想重新格式化单元格时,它似乎会重新格式化单元格。我希望它以计划文本的形式通过。我已经阅读了一些解决这个问题的方法,并且已经实现了它们,但是我仍然有同样的问题 读取器将日期转换为数字,并将数字转换为日期 例如: 2016年1月29日,星期五:42398 及 40.00的时间是:1900年2月9日12:00:00 代码: 我试过了C# 从Excel读取列,重新格式化单元格,c#,excel,oledbconnection,oledbdataadapter,C#,Excel,Oledbconnection,Oledbdataadapter,我目前正在尝试从excel电子表格中读取单元格,当我不想重新格式化单元格时,它似乎会重新格式化单元格。我希望它以计划文本的形式通过。我已经阅读了一些解决这个问题的方法,并且已经实现了它们,但是我仍然有同样的问题 读取器将日期转换为数字,并将数字转换为日期 例如: 2016年1月29日,星期五:42398 及 40.00的时间是:1900年2月9日12:00:00 代码: 我试过了 IMEX=0; HDR=NO; TypeGuessRows=1; 这就是我打印表格的方式 public void
IMEX=0;
HDR=NO;
TypeGuessRows=1;
这就是我打印表格的方式
public void printdt(DataTable dt) {
int counter1 = 0;
int counter2 = 0;
string temp = "";
foreach (DataRow dataRow in dt.Rows) {
foreach (var item in dataRow.ItemArray) {
temp += " ["+counter1+"]["+counter2+"]"+ item +", ";
counter2++;
}
counter1++;
logger.Debug(temp);
temp = "";
counter2 = 0;
}
}
我有一个类似的问题,除了使用Interop读取Excel电子表格。这对我很有用:
var value = (range.Cells[rowCnt, columnCnt] as Range).Value2;
string str = value as string;
DateTime dt;
if (DateTime.TryParse((value ?? "").ToString(), out dt))
{
// Use the cell value as a datetime
}
编辑以添加新想法
我打算建议将电子表格保存为逗号分隔的值。然后Excel将单元格转换为文本。在C中解析CSV很容易
这让我想到了如何以编程方式进行转换,这在中有介绍。可能您正在寻找的是公认答案中的代码:
string ExcelFilename = "c:\\ExcelFile.xls";
DataTable worksheets;
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + ExcelFilename + ";" + @"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
worksheets = connection.GetSchema("Tables");
foreach (DataRow row in worksheets.Rows)
{
// For Sheets: 0=Table_Catalog,1=Table_Schema,2=Table_Name,3=Table_Type
// For Columns: 0=Table_Name, 1=Column_Name, 2=Ordinal_Position
string SheetName = (string)row[2];
OleDbCommand command = new OleDbCommand(@"SELECT * FROM [" + SheetName + "]", connection);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
oleAdapter.SelectCommand = command;
DataTable dt = new DataTable();
oleAdapter.FillSchema(dt, SchemaType.Source);
oleAdapter.Fill(dt);
for (int r = 0; r < dt.Rows.Count; r++)
{
string type1 = dr[1].GetType().ToString();
string type2 = dr[2].GetType().ToString();
string type3 = dr[3].GetType().ToString();
string type4 = dr[4].GetType().ToString();
string type5 = dr[5].GetType().ToString();
string type6 = dr[6].GetType().ToString();
string type7 = dr[7].GetType().ToString();
}
}
}
这没用。我不想重新转换它。我希望它以正确的格式从单元格中出来。或者就像计划文本一样。在我看来,如果你想把一个单元格作为一个字符串来读取,那么它需要作为一个字符串来输入,而不是作为一个日期时间或者其他什么。您可能需要更改电子表格并将这些单元格转换为文本,可能需要在值的前面加上一个引号。这可能不是你想要的答案。
string ExcelFilename = "c:\\ExcelFile.xls";
DataTable worksheets;
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + ExcelFilename + ";" + @"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
worksheets = connection.GetSchema("Tables");
foreach (DataRow row in worksheets.Rows)
{
// For Sheets: 0=Table_Catalog,1=Table_Schema,2=Table_Name,3=Table_Type
// For Columns: 0=Table_Name, 1=Column_Name, 2=Ordinal_Position
string SheetName = (string)row[2];
OleDbCommand command = new OleDbCommand(@"SELECT * FROM [" + SheetName + "]", connection);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
oleAdapter.SelectCommand = command;
DataTable dt = new DataTable();
oleAdapter.FillSchema(dt, SchemaType.Source);
oleAdapter.Fill(dt);
for (int r = 0; r < dt.Rows.Count; r++)
{
string type1 = dr[1].GetType().ToString();
string type2 = dr[2].GetType().ToString();
string type3 = dr[3].GetType().ToString();
string type4 = dr[4].GetType().ToString();
string type5 = dr[5].GetType().ToString();
string type6 = dr[6].GetType().ToString();
string type7 = dr[7].GetType().ToString();
}
}
}