该Demo实现将多个TXT文件合并到同一个XLSX文件同一个SHEET中,相应的对TXT文件的格式也是有要求的,主要针对需要做数据统计的XLSX文件,要求TXT文件中的列相邻数据之间有明确且统一的分离标识字符,每行数据的列数和列标题个数对应
需要引用的程序集:https://download.csdn.net/detail/qq_39040135/10704181
说明:.NET Framework4.0
/// <summary>
/// Txt.Format ChangeTo Excel
/// </summary>
/// <param name="savepath"></param> excel要存放的完整路径(X:\xx\xxx\xxxx.xlsx)
/// <param name="sheetname"></param> 建立的工作表名称
/// <param name="Source"></param> 搜寻目录
/// <param name="flag"></param> txt文件中的字符串分离标识字符
/// <param name="ExcelTittle[]"></param> 列标题字符串集合
/// <param name="patterns[]"></param> 要搜寻的指定文件类型集合"xxx*.txt",“xxx”指文件名中包含的重要字符串
public static void TxtToXlsx(string Source,string sheetname, char flag, string[] patterns, string SavePath,string[] ExcelTittle)
{
int index = 0;
foreach (string pattern in patterns)
{
try
{
string[] filenames = Directory.GetFiles(Source, pattern, SearchOption.AllDirectories);
//下面这句是根据文件名中包含的关键字符串进行筛选文件的方法,详细可以看我的另一篇博客“C# orderby排序在文件操作中的应用”
/*
fileoperation.dateorder filenames_result = fileoperation.FileSelectByName(filenames, Dstart, Dend, 8, 8);
*/
if (filenames_result.filenames.Length != 0 && !Array.Exists(filenames_result.filenames, string.IsNullOrEmpty))
{
string savePath = SavePath + @"\" + Names[index] + filenames_result.date+ ".xlsx";
XSSFWorkbook WorkBook = new XSSFWorkbook();
WorkBook.CreateSheet(sheetname);
ISheet sheet = WorkBook.GetSheetAt(0); //sheet.SetColumnWidth();
int StartRow = 0;
foreach (string file in filenames_result.filenames)
{
int RowNumber = 0;
int ColumnNmuber = 0;
List<string> Contant = new List<string>();
StreamReader sr = new StreamReader(file);
string ReadLine = sr.ReadLine();
FileInfo f =new FileInfo(file);
string[] p = ReadLine.Split(flag);
ColumnNmuber = p.Length;
while (ReadLine != null)
{
RowNumber++;
Contant.Add(ReadLine);
ReadLine = sr.ReadLine();
}
//创建单元格,先行后列
for (int j = StartRow; j < StartRow + RowNumber+1; j++)
{
sheet.CreateRow(j);
XSSFRow Row = (XSSFRow)sheet.GetRow(j);
for (int k = 0; k < ColumnNmuber; k++)
{
Row.CreateCell(k);
}
}
//往单元格写数据
for (int j = StartRow; j < RowNumber + StartRow+1; j++)
{
string[] CellValue = Contant[j - StartRow].Split(flag);
for (int k = 0; k < ColumnNmuber; k++)
{
if (j == 0)
{
sheet.GetRow(j).GetCell(k).SetCellValue(ExcelTittle[k]);
}
else
{
if (CellValue[k].Contains(".") && !CellValue[k].Contains("/"))
{
sheet.GetRow(j).GetCell(k).SetCellValue(Convert.ToDouble(CellValue[k]));
}
else
{
sheet.GetRow(j).GetCell(k).SetCellValue(CellValue[k]);
}
}
}
}
StartRow += RowNumber;
}
sheet.GetRow(0).CreateCell(sheet.GetRow(0).LastCellNum);
sheet.GetRow(0).GetCell(sheet.GetRow(0).LastCellNum - 1).SetCellFormula("CORREL(D:D,F:F)");
sheet.GetRow(0).CreateCell(sheet.GetRow(0).LastCellNum);
sheet.GetRow(0).GetCell(sheet.GetRow(0).LastCellNum - 1).SetCellFormula("CORREL(G:G,H:H)");
sheet.GetRow(0).CreateCell(sheet.GetRow(0).LastCellNum);
sheet.GetRow(0).GetCell(sheet.GetRow(0).LastCellNum - 1).SetCellFormula("RSQ(F:F,D:D)");
sheet.GetRow(0).CreateCell(sheet.GetRow(0).LastCellNum);
sheet.GetRow(0).GetCell(sheet.GetRow(0).LastCellNum - 1).SetCellFormula("RSQ(H:H,G:G)");
//不存在目录就创建一个
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
FileStream file2007 = new FileStream(savePath, FileMode.Create);
WorkBook.Write(file2007);
file2007.Close();
WorkBook.Close();
MainView.frm.listBox2.Items.Add(string.Format( "Xlsx保存成功 '{0}' ", savePath) + "\t" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
}
else
{
//MainView是我的主窗体名称,在主窗体代码中使用了如下方式,实现在其他类中对主窗体控件的访问。
/*
public static MainView frm;
public MainView()
{
InitializeComponent();
frm = this;
}*/
MainView.frm.listBox2.Items.Add( "Xlsx保存失败" + "未找到源文件" + "\t" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
}
index++;
}
catch (Exception ex)
{
MainView.frm.listBox2.Items.Add( "Xlsx保存失败" + ex.Message + "\t" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
index++;
}
}
}