博客链接:
http://www.cnblogs.com/atao/category/209358.html
http://blog.csdn.net/zhumi/article/details/49076
http://tonyqus.sinaapp.com/tutorial
官网:http://npoi.codeplex.com/
NPOI官方网站:http://www.npoi.info/npoi2tutorial (教程系列)
示例:
using ExcelManager.Model; using NPOI.HSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace ExcelManager.Helper { public class ExportHelper { /// <summary> /// 导出入库表格 /// </summary> /// <param name="List"></param> public static void ExportStorage(List<UserModel> List) { if (List.Count == 0) { return; } try { HSSFWorkbook hssfworkbook = new HSSFWorkbook(); HSSFSheet sheet = (HSSFSheet)hssfworkbook.CreateSheet("Sheet1"); sheet.SetColumnWidth(0, 256 * 15);//256*legth 设置列宽 sheet.SetColumnWidth(1, 256 * 30); //创建列名 HSSFRow rowHeader = (HSSFRow)sheet.CreateRow(0); rowHeader.CreateCell(0).SetCellValue("编号"); rowHeader.CreateCell(1).SetCellValue("姓名"); //创建表头 HSSFRow rowContent = null; int rowIndex = 1; foreach (var item in List) { rowContent = (HSSFRow)sheet.CreateRow(rowIndex++); rowContent.CreateCell(0).SetCellValue(item.ID); rowContent.CreateCell(1).SetCellValue(item.Name); } SaveExcel("测试", hssfworkbook); } catch (Exception ex) { LogUtil.Log("导出异常:" + ex.ToString()); MessageBox.Show("导出失败"); } } #region 保存文件 /// <summary> /// 保存文件 /// </summary> /// <param name="fileNamePrefix">文件名前缀</param> /// <param name="hssfworkbook">HSSFWorkbook</param> private static void SaveExcel(string fileNamePrefix, HSSFWorkbook hssfworkbook) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Excel 工作表 (*.xls)|*.xls"; dialog.RestoreDirectory = true; dialog.FileName = fileNamePrefix + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"); if (dialog.ShowDialog() == DialogResult.OK) { string path = dialog.FileName.ToString(); using (FileStream file = new FileStream(path, FileMode.Create)) { hssfworkbook.Write(file); } MessageBox.Show("导出完成"); } } #endregion } }
将excel保存完后,如何下载文件到本地?