NPOI 帮助

1、导入Excel
 //通过SaveFileDialog类弹出一个保存对话框

            SaveFileDialog sfd = new SaveFileDialog();

            //设置文件的保存类型,默认选中Excel文件

            sfd.Filter = "Excel文件|*.xls";

            //设置默认保存文件名称

            sfd.FileName = "学生信息表";

            //如果用户点击了保存对话框的确定按钮

            if (sfd.ShowDialog()==DialogResult.OK)

            {

                //获取到Excel文件名

                string filename = sfd.FileName;

                //获取学生列表

                //List<Student> list = dataGridView1.DataSource as List<Student>;

                //内存中创建一个空的Excel文件

               HSSFWorkbook workbook = new HSSFWorkbook();

              

                //在Excel文件上通过对HSSFSheet创建一个工作表

                HSSFSheet sheet = workbook.CreateSheet("students");

                //给工作表上添加一行

                HSSFRow row1 = sheet.CreateRow(0);

                //在添加的航上创建一个列

                HSSFCell cell1 = row1.CreateCell(0,HSSFCell.CELL_TYPE_STRING);

                //设置该列的值

                cell1.SetCellValue("学号");

 

                cell1 = row1.CreateCell(1, HSSFCell.CELL_TYPE_STRING);

                cell1.SetCellValue("学生姓名");

 

                cell1 = row1.CreateCell(2, HSSFCell.CELL_TYPE_STRING);

                cell1.SetCellValue("家庭住址");

 

                cell1 = row1.CreateCell(3, HSSFCell.CELL_TYPE_STRING);

                cell1.SetCellValue("出生日期");

                //遍历dataGridView中的所有列,然后将列添加到Excel工作表中

                for (int i = 1; i <=dataGridView1.Rows.Count; i++)

                {

                    //设置字体

                    HSSFFont font = workbook.CreateFont();

                    //字体名称

                    font.FontName = "华文行楷";

                    //设置字体大小

                    font.FontHeightInPoints = 25;

 

                    //设置列的样式

                    HSSFCellStyle style1 = workbook.CreateCellStyle();

                    //设置列的背景色

                    style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.ORANGE.index;

                    //设置填充边框样式

                    style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

                    //设置字体显示样式

                    style1.SetFont(font);

 

                    HSSFCellStyle style2 = workbook.CreateCellStyle();

                    style2.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.YELLOW.index;

                    style2.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

                  

 

                    HSSFRow row = sheet.CreateRow(i);

                    HSSFCell cell = row.CreateCell(0, HSSFCell.CELL_TYPE_NUMERIC);

                    cell.CellStyle = style1;

                    cell.SetCellValue(dataGridView1.Rows[i - 1].Cells[0].Value.ToString());

 

                    cell = row.CreateCell(1,HSSFCell.CELL_TYPE_STRING);

                    cell.CellStyle = style1;

                    cell.SetCellValue(dataGridView1.Rows[i-1].Cells[1].Value.ToString());

 

                    cell = row.CreateCell(2, HSSFCell.CELL_TYPE_STRING);

                    cell.CellStyle = style2;

                    cell.SetCellValue(dataGridView1.Rows[i-1].Cells[2].Value.ToString());

 

                    cell = row.CreateCell(3, HSSFCell.CELL_TYPE_STRING);

                    cell.CellStyle = style2;

                    cell.SetCellValue(dataGridView1.Rows[i-1].Cells[3].Value.ToString());

                }

                using (FileStream fs=new FileStream(filename,FileMode.OpenOrCreate))

                {

                    //将内容写入到硬盘中

                    workbook.Write(fs);

                }

                MessageBox.Show("导出成功!");

            }
View Code
2、导出Excel
void InitializeWorkbook(string path)

        {

            //read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.

            //book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added.

            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))

            {

                hssfworkbook = new HSSFWorkbook(file);

            }

        }

        DataSet ds = new DataSet();

        void ConvertToDataTable()

        {

            HSSFSheet sheet = hssfworkbook.GetSheetAt(0);

            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

           

            DataTable dt = new DataTable();

            for (int j = 0; j < 5; j++)

            {

                dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());

            }

 

            while (rows.MoveNext())

            {

                HSSFRow row = (HSSFRow)rows.Current;

                DataRow dr = dt.NewRow();

 

                for (int i = 0; i < row.LastCellNum; i++)

                {

                    HSSFCell cell = row.GetCell(i);

                    if (cell == null)

                    {

                        dr[i] = null;

                    }

                    else

                    {

                        dr[i] = cell.ToString();

                    }

                }

                dt.Rows.Add(dr);

            }

            ds.Tables.Add(dt);

        }

        //从Excel导入

        private void button2_Click(object sender, EventArgs e)

        {

            InitializeWorkbook("学生信息表.xls");

            ConvertToDataTable();

 

           dgvexport.DataSource= ds.Tables[0];

        }
View Code
原文地址:https://www.cnblogs.com/gldblogs/p/4143309.html