我的文本文件包含如下数据:
ID 1 Name Mary Mark 89
ID 2 Name Mai Mark 67
我有另一个包含数据的文本文件:
ID Mobile
1 0993209320
2 0943043094
我想创建只包含两列的 excel 文件:
Mobile Message
0993209320 ID 1 Name Mary Mark 89
0943043094 ID 2 Name Mai Mark 67
为此,我尝试了以下代码,它工作正常,并创建了所需的 excel 文件,但是当我将两个文本文件中包含的数据更改为阿拉伯语时,代码创建了 excel 文件,但是为空,我该怎么办修改代码使其能够处理阿拉伯字符??
这是代码:
DataTable dtMapping = new DataTable();
ArrayList lstMapping = new ArrayList();
dtMapping.Columns.Add("Mobile"); dtMapping.Columns.Add("Message");
string[] readFile1 = File.ReadAllLines(Location of 1st file);
string[] readFile2 = File.ReadAllLines(Location of 2nd file);
for (int i = 0; i < readFile1.Length; i++)
{
string fileData = readFile1[i].ToString();
int find_Id_Index = fileData.IndexOf("ID ");
if (find_Id_Index != -1)
{
int find_Name_Index = fileData.IndexOf("Name ", find_Id_Index);
string id = fileData.Substring(find_Id_Index+3,find_Name_Index-(find_Id_Index+3) ).Trim();
lstMapping.Add("ID " + id);
lstMapping.Add(fileData.Substring(find_Name_Index, fileData.Length - (find_Name_Index)).Trim());
}
}
for (int i = 1; i < readFile2.Length; i++)
{
int find_Space_Index = readFile2[i].IndexOf(" ");
if (find_Space_Index != -1)
{
string file2id = readFile2[i].Substring(0, find_Space_Index).Trim();
string mobileno = readFile2[i].Substring(find_Space_Index, readFile2[2].Length - (find_Space_Index - file2id.Length+1)).Trim();
int find_Id = lstMapping.IndexOf("ID " + file2id);
if (find_Id != -1)
{
dtMapping.Rows.Add(mobileno, "ID " + file2id + " " + lstMapping[find_Id + 1]);
}
}
}
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
for (int i = 0; i < dtMapping.Columns.Count; i++)
{
workSheet.Cells[1, (i + 1)] = dtMapping.Columns[i].ColumnName;
}
for (int i = 0; i < dtMapping.Rows.Count; i++)
{
for (int j = 0; j < dtMapping.Columns.Count; j++)
{
workSheet.Cells[(i + 2), (j + 1)] = dtMapping.Rows[i][j];
}
}
excelApp.Visible = true;