码迷,mamicode.com
首页 > 其他好文 > 详细

npoi

时间:2014-08-05 13:25:39      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   io   数据   for   

NPOI可以很方便的读取写入EXCEL,且对客户机没有要求

读取excel的实例代码

 1  using (FileStream file = new FileStream("可约医院列表(网址).xls", FileMode.Open, FileAccess.Read))//参数为地址和打开方式操作方式
 2             {
 3                 HSSFWorkbook h = new HSSFWorkbook(file); 
 4                 HSSFSheet sheettemp = (HSSFSheet)h.GetSheet("sheet1");
 5                 for (int i = 1; i < sheettemp.LastRowNum; i++)
 6                 {
 7                     url = sheettemp.GetRow(i).GetCell(2).ToString();
 8                    ....  具体操作13                 }
14             }

可以当做一个datatable来操作 通过行和列来读取数据。

 

写入excel代码:

       HSSFWorkbook h = new HSSFWorkbook();
            HSSFSheet sheettemp = (HSSFSheet)h.CreateSheet("sheet1"); //创建一个对象 
Row _row = sheettemp.CreateRow(0); _row.CreateCell(0).SetCellValue("医院"); _row.CreateCell(1).SetCellValue("科室"); _row.CreateCell(2).SetCellValue("姓名"); _row.CreateCell(3).SetCellValue("擅长"); _row.CreateCell(4).SetCellValue("简介"); _row.CreateCell(5).SetCellValue("头像图片");    //excel的列头      for (int i = 0; i < dt.Rows.Count; i++) {
         //具体赋值 Row row = sheettemp.CreateRow(i + 1); row.CreateCell(0).SetCellValue(dt.Rows[i][0].ToString()); row.CreateCell(1).SetCellValue(dt.Rows[i][1].ToString()); row.CreateCell(2).SetCellValue(dt.Rows[i][2].ToString()); row.CreateCell(3).SetCellValue(dt.Rows[i][3].ToString()); row.CreateCell(4).SetCellValue(dt.Rows[i][4].ToString()); row.CreateCell(5).SetCellValue(dt.Rows[i][5].ToString()); AddPieChart(sheettemp, h, dt.Rows[i][5].ToString(), (i + 1), 5); }
sheettemp.ForceFormulaRecalculation = true;//请加上这句话 using (FileStream filewrite = new FileStream("C:\\挂号网.xls", FileMode.Create))//保存路径 { h.Write(filewrite); filewrite.Close(); }

 通过遍历 给excel赋值后 可以直接保存。这里路径可以要求用户选择或配置

 

npoi导出excel中添加图片

       try
            {
                string FileName = fileurl;
                byte[] bytes = System.IO.File.ReadAllBytes(FileName);
                if (!string.IsNullOrEmpty(FileName))
                {
                    int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
                    HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 50, col, row, col + 1, row + 1);
                    //##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50
                    HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

                    // pict.Resize();这句话一定不要,这是用图片原始大小来显示
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

方法为:AddPieChart(HSSFSheet sheet, HSSFWorkbook workbook, string fileurl, int row, int col)

fileurl为你保存后的地址不可以使用网页上的引用地址。

npoi,布布扣,bubuko.com

npoi

标签:style   blog   color   使用   os   io   数据   for   

原文地址:http://www.cnblogs.com/tanpangpang/p/3891720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!