1、用到的命名空间
using Excel;
using System.Diagnostics;
using NPOI.HSSF.UserModel;
npoi读取excel
1 StringBuilder sb = new StringBuilder(); 2 using (Stream stream = File.OpenRead("c:\\abc.xls")) 3 { 4 //内容中的excel文件 5 HSSFWorkbook workbook = new HSSFWorkbook(stream); 6 //Console.WriteLine(workbook.GetSheetName(0)); 7 //表格 8 HSSFSheet sheet = workbook.GetSheetAt(0); 9 //最后一行的索引 10 //sheet.LastRowNum 11 for (int i = 1; i <= sheet.LastRowNum; i++) 12 { 13 //表格中的每一行 14 HSSFRow row = sheet.GetRow(i); 15 16 //row.LastCellNum 单元格的个数 17 for (int j = 0; j < row.LastCellNum; j++) 18 { 19 //单元格 20 HSSFCell cell = row.GetCell(j); 21 //判断单元格是否为null 22 if (cell == null) 23 { 24 sb.Append(","); 25 continue; 26 } 27 //判断单元格的类型 28 if (cell.CellType == HSSFCell.CELL_TYPE_NUMERIC) 29 { 30 sb.Append(cell.NumericCellValue.ToString() + ","); 31 } 32 else if (cell.CellType == HSSFCell.CELL_TYPE_STRING) 33 { 34 sb.Append(cell.StringCellValue + ","); 35 } 36 } 37 sb.Append("\r\n"); 38 } 39 } 40 Console.WriteLine(sb.ToString());
npoi 创建excel
1 //在内容中创建excel文件 2 HSSFWorkbook workbook = new HSSFWorkbook(); 3 4 //在workbook中创建sheet 5 HSSFSheet sheet = workbook.CreateSheet(); 6 7 //在sheet中创建行 8 HSSFRow row = sheet.CreateRow(0); 9 //在行中创建第一个单元格 10 HSSFCell cell = row.CreateCell(0,HSSFCell.CELL_TYPE_STRING); 11 cell.SetCellValue("张三"); 12 // 13 cell = row.CreateCell(1, HSSFCell.CELL_TYPE_NUMERIC); 14 cell.SetCellValue(18); 15 //保存到文件 16 using (FileStream fs = new FileStream("c:\\22222222.xls",FileMode.Create)) 17 { 18 workbook.Write(fs); 19 } 20 Console.WriteLine("over");
2、本机创建邮件服务器
邮件 对象
1 MailMessage msg = new MailMessage(); 2 msg.From = new MailAddress("abc@163.com","纯洁的...."); 3 msg.To.Add(new MailAddress("xyz@126.com","dear Zi")); 4 msg.Subject = txtTitle.Text.Trim(); 5 msg.Body = txtContent.Text.Trim(); 6 //发送邮件 7 SmtpClient client = new SmtpClient("127.0.0.1");//;("127.0.0.1");//smtp.126.com 8 //设置发送服务器登录 的用户名和密码 9 client.Credentials = new NetworkCredential("nll", "admin");//这里的用户名必须是From发件人的用户 10 11 client.Send(msg);//发送 12