文本文件信息导入Excel中(NPOI方式)
时间:2010-10-25 来源:凭栏处
代码
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文件
}
相关阅读 更多 +