这是我在这里的第一篇文章,总的来说 - 我对C#很新,所以请原谅我任何错误。 我正在努力做到以下几点:
感谢您的帮助!
这是我正在运行的代码。 有问题的部分是代码末尾的 ReadExcel 类:
public class RecursiveFileProcessor
{
static void Main()
{
string InputMainFolder = "C:\\US\\";
string OutputMainFolder = "C:\\BAJ\\";
foreach (string InputFile in GetFiles(InputMainFolder))
{
Console.WriteLine(InputFile);
string InputPath = Path.GetDirectoryName(InputFile);
string InputFileName = Path.GetFileName(InputFile);
string OuputPath = InputPath.Replace(InputMainFolder, OutputMainFolder);
string OutputFile = System.IO.Path.Combine(OuputPath, InputFileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(OuputPath))
{
System.IO.Directory.CreateDirectory(OuputPath);
//Console.WriteLine("Ouput folder " + OuputPath + "created.");
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
if (File.Exists(OutputFile))
{
Console.WriteLine("ERROR: File Already Exists: " + OutputFile);
continue;
}
else
{
System.IO.File.Copy(InputFile, OutputFile, true);
}
//Try to catch an exception
try
{
//Here comes some important part of my code (that works fine)
}
catch (Exception exe)
{
//call LogFile method and pass argument as Exception message, event name, control name, error line number, current form name
LogFile(exe.Message, exe.ToString(), exe.LineNumber(), InputFile, OutputMainFolder);
}
}
//call excel reader for tests
//THIS IS THE PLACE WHERE I WILL CALL MY EXCEL READER TO GET DATA INTO ARRAY/LIST/DATASET ETC
{
string MappingFile = "c:\\attributes mapping\\attributes mapping.xlsx";
ReadExcel.ReadExcelToTable(MappingFile);
}
//That's the final line of the code
Console.WriteLine("DONE: " + DateTime.Now.ToUniversalTime() + " UTC");
Console.ReadLine();
}
static IEnumerable<string> GetFiles(string path)
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0)
{
path = queue.Dequeue();
try
{
foreach (string subDir in Directory.GetDirectories(path))
{
queue.Enqueue(subDir);
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
}
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
}
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
yield return files[i];
}
}
}
}
//This function writes exception details to the file in the output folder
public static void LogFile(string sExceptionName, string sEventName, int nErrorLineNo, string sFileName, string sOutputFolder)
{
StreamWriter log;
if (!File.Exists(sOutputFolder + "logfile.txt"))
{
log = new StreamWriter(sOutputFolder + "logfile.txt");
}
else
{
log = File.AppendText(sOutputFolder + "logfile.txt");
}
// Write to the file:
log.WriteLine("File Name:" + sFileName);
log.WriteLine("Data Time:" + DateTime.Now);
log.WriteLine("Error Line No.:" + nErrorLineNo);
log.WriteLine("Exception Name:" + sExceptionName);
log.WriteLine("Event Name:" + sEventName);
log.WriteLine("--------------------------------------------------------------------------");
// Close the stream:
log.Close();
}
}
//This part of code gets a line number where the exception appeared
public static class ExceptionHelper
{
public static int LineNumber(this Exception e)
{
int linenum = 0;
try
{
linenum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(":line") + 5));
}
catch
{
//Stack trace is not available!
}
return linenum;
}
}
好的,我已设法将这些数据导入DataTables:
//Reads excel file and creates data sets from each excel sheet
public static DataSet ReadExcelToTable(string FileName)
{
//string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\'Excel 8.0;HDR=YES;IMEX=1';";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\'Excel 8.0;HDR=YES;IMEX=1';";
DataSet output = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow schemaRow in schemaTable.Rows)
{
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
try
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;
DataTable outputTable = new DataTable(sheet);
output.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
}
catch (Exception ex)
{
throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
}
}
}
}
return output;
这些DataTables(3)必须在代码执行结束时可用。
ExcelReader将返回3个DataTables作为输出,对吗? 然后我必须在我调用ExcelReader类的地方之后继续使用这3个DataTables作为输出。
现在我必须做那样的事情:
额外信息:ID列中的ID是唯一字符串,只能返回一个Name和一个New_Id(字符串)。
有什么想法吗?