文本文件信息导入Excel中(NPOI方式,只提取公司名称、手机号码)
文本文件格式:
代码
1 山东华德城市投资有限公司 13793695297
2 山东钢联物流有限公司 8901343
3 山东潍坊福田模具有限责任公司 0536-7608638
4 潍坊市临朐矿兴轻钙股份有限公司 3610649
5 潍坊金宝新型建材有限公司 8804346
6 潍坊中外运仓储物流有限公司 053282897780 13805326133
2 山东钢联物流有限公司 8901343
3 山东潍坊福田模具有限责任公司 0536-7608638
4 潍坊市临朐矿兴轻钙股份有限公司 3610649
5 潍坊金宝新型建材有限公司 8804346
6 潍坊中外运仓储物流有限公司 053282897780 13805326133
实现代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void btnBegin_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(@"F:\\ComTel.txt", System.Text.Encoding.GetEncoding("gb2312"));
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet("企业联系方式");
HSSFRow row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("序号");
row.CreateCell(1).SetCellValue("公司名称");
row.CreateCell(2).SetCellValue("联系方式");
int i = 1;
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
string[] lines = str.Trim().Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
string[] data = line.Split(new string[] { " ", " " }, StringSplitOptions.RemoveEmptyEntries);
string mobile = string.Empty;
string num = data[0];
string name = data[1];
try
{
if (data[2].Length == 11)
mobile = data[2];
else
{
try
{
if (data[3].Length == 11)
mobile = data[3];
}
catch
{
continue;
}
}
}
catch (Exception)
{
continue;
}
HSSFRow newRow = sheet.CreateRow(i);
newRow.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(i.ToString());
newRow.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue(name);
newRow.CreateCell(2, HSSFCell.CELL_TYPE_STRING).SetCellValue(mobile);
i++;
}
}
sr.Close();
string filename = @"F:\\公司联系方式.xls";
using (FileStream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
workbook.Write(stream);
}
MessageBox.Show(this.Page, "导出完毕");
System.Diagnostics.Process.Start(filename);//自动打开Excel文件
}
{
StreamReader sr = new StreamReader(@"F:\\ComTel.txt", System.Text.Encoding.GetEncoding("gb2312"));
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet("企业联系方式");
HSSFRow row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("序号");
row.CreateCell(1).SetCellValue("公司名称");
row.CreateCell(2).SetCellValue("联系方式");
int i = 1;
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
string[] lines = str.Trim().Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
string[] data = line.Split(new string[] { " ", " " }, StringSplitOptions.RemoveEmptyEntries);
string mobile = string.Empty;
string num = data[0];
string name = data[1];
try
{
if (data[2].Length == 11)
mobile = data[2];
else
{
try
{
if (data[3].Length == 11)
mobile = data[3];
}
catch
{
continue;
}
}
}
catch (Exception)
{
continue;
}
HSSFRow newRow = sheet.CreateRow(i);
newRow.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(i.ToString());
newRow.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue(name);
newRow.CreateCell(2, HSSFCell.CELL_TYPE_STRING).SetCellValue(mobile);
i++;
}
}
sr.Close();
string filename = @"F:\\公司联系方式.xls";
using (FileStream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
workbook.Write(stream);
}
MessageBox.Show(this.Page, "导出完毕");
System.Diagnostics.Process.Start(filename);//自动打开Excel文件
}