日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NPOI操作Excel

發布時間:2024/9/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NPOI操作Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Npoi?簡介


1.整個Excel表格叫做工作表:WorkBook(工作薄),包含的叫頁(工作表):Sheet;行:Row;單元格Cell。

2.Npoi?下載地址:http://npoi.codeplex.com/releases/view/38113

3.npoi 能夠讀寫幾乎所有的Office 97-2003文件格式,至少能夠支持Word, PowerPoint, Excel, Visio的格式。

4.NPOI常用于將查詢結果以Excel格式的形式保存到本地磁盤。


使用Npoi創建一個簡單的xls文件

private void btnCreate_Click(object sender, EventArgs e){//創建工作薄HSSFWorkbook wk = new HSSFWorkbook();//創建一個名稱為mySheet的表ISheet tb = wk.CreateSheet("mySheet");//創建一行,此行為第二行IRow row1 = tb.CreateRow(0);IRow row2 = tb.CreateRow(1);IRow row3 = tb.CreateRow(2);ArrayList randomNumArray = new ArrayList();for (int i = 0; i < 20; i++){ICell row1cell = row1.CreateCell(i); //在第二行中創建單元格row1cell.SetCellValue(i);//循環往第二行的單元格中添加數據ICell row2cell = row2.CreateCell(i); //在第二行中創建單元格row2cell.SetCellValue(20-i);//循環往第二行的單元格中添加數據Random random = new Random();double randomNum = random.Next(1000); while (!randomNumArray.Contains(randomNum)){randomNumArray.Add(randomNum);//randomNum = random.Next(1000); }ICell row3cell = row3.CreateCell(i);row3cell.SetCellValue(randomNum);}using (FileStream fs = File.OpenWrite(@"F:/ExcelCreateByNPOI4.xls")) //打開一個xls文件,如果沒有則自行創建,如果存在myxls.xls文件則在創建是不要打開該文件! {wk.Write(fs); //向打開的這個xls文件中寫入mySheet表并保存。MessageBox.Show("提示:創建成功!");}}

使用Npoi讀取一個簡單的xls文件

//讀取xls文件private void button2_Click(object sender, EventArgs e){ StringBuilder sbr = new StringBuilder();using (FileStream fs = File.OpenRead(@"c:/myxls.xls")) //打開myxls.xls文件 {HSSFWorkbook wk = new HSSFWorkbook(fs); //把xls文件中的數據寫入wk中for (int i = 0; i < wk.NumberOfSheets; i++) //NumberOfSheets是myxls.xls中總共的表數 {ISheet sheet = wk.GetSheetAt(i); //讀取當前表數據for (int j = 0; j <= sheet.LastRowNum; j++) //LastRowNum 是當前表的總行數 {IRow row = sheet.GetRow(j); //讀取當前行數據if (row != null){sbr.Append("-------------------------------------\r\n"); //讀取行與行之間的提示界限for (int k = 0; k <= row.LastCellNum; k++) //LastCellNum 是當前行的總列數 {ICell cell = row.GetCell(k); //當前表格if (cell != null){ sbr.Append(cell.ToString()); //獲取表格中的數據并轉換為字符串類型 }}}}} }sbr.ToString();using (StreamWriter wr = new StreamWriter(new FileStream(@"c:/myText.txt", FileMode.Append))) //把讀取xls文件的數據寫入myText.txt文件中 {wr.Write(sbr.ToString());wr.Flush();}}

使用Npoi創建一個常用的xls文件

//創建一個常用的xls文件private void button3_Click(object sender, EventArgs e){ IWorkbook wb = new HSSFWorkbook();//創建表 ISheet sh = wb.CreateSheet("zhiyuan");//設置單元的寬度 sh.SetColumnWidth(0, 15 * 256);sh.SetColumnWidth(1, 35 * 256);sh.SetColumnWidth(2, 15 * 256);sh.SetColumnWidth(3, 10 * 256);int i = 0;#region 練習合并單元格sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3));//CellRangeAddress()該方法的參數次序是:開始行號,結束行號,開始列號,結束列號。 IRow row0 = sh.CreateRow(0);row0.Height = 20 * 20;ICell icell1top0 = row0.CreateCell(0);icell1top0.CellStyle = Getcellstyle(wb, stylexls.頭);icell1top0.SetCellValue("標題合并單元");#endregioni++;#region 設置表頭IRow row1 = sh.CreateRow(1);row1.Height = 20 * 20;ICell icell1top = row1.CreateCell(0);icell1top.CellStyle = Getcellstyle(wb, stylexls.頭);icell1top.SetCellValue("網站名");ICell icell2top = row1.CreateCell(1);icell2top.CellStyle = Getcellstyle(wb, stylexls.頭);icell2top.SetCellValue("網址");ICell icell3top = row1.CreateCell(2);icell3top.CellStyle = Getcellstyle(wb, stylexls.頭);icell3top.SetCellValue("百度快照");ICell icell4top = row1.CreateCell(3);icell4top.CellStyle = Getcellstyle(wb, stylexls.頭);icell4top.SetCellValue("百度收錄");#endregion using(FileStream stm=File.OpenWrite(@"c:/myMergeCell.xls")){wb.Write(stm); MessageBox.Show("提示:創建成功!");}}#region 定義單元格常用到樣式的枚舉public enum stylexls{頭,url,時間,數字,錢,百分比,中文大寫,科學計數法,默認}#endregion#region 定義單元格常用到樣式static ICellStyle Getcellstyle(IWorkbook wb, stylexls str){ICellStyle cellStyle = wb.CreateCellStyle();//定義幾種字體 //也可以一種字體,寫一些公共屬性,然后在下面需要時加特殊的 IFont font12 = wb.CreateFont();font12.FontHeightInPoints = 10;font12.FontName = "微軟雅黑";IFont font = wb.CreateFont();font.FontName = "微軟雅黑";//font.Underline = 1;下劃線 IFont fontcolorblue = wb.CreateFont();fontcolorblue.Color = HSSFColor.OLIVE_GREEN.BLUE.index;fontcolorblue.IsItalic = true;//下劃線 fontcolorblue.FontName = "微軟雅黑";//邊框 cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.HAIR;cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR;cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.DOTTED;//邊框顏色 cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index;cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index;//背景圖形,我沒有用到過。感覺很丑 //cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; cellStyle.FillForegroundColor = HSSFColor.WHITE.index;// cellStyle.FillPattern = FillPatternType.NO_FILL; cellStyle.FillBackgroundColor = HSSFColor.BLUE.index;//水平對齊 cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;//垂直對齊 cellStyle.VerticalAlignment = VerticalAlignment.CENTER;//自動換行 cellStyle.WrapText = true;//縮進;當設置為1時,前面留的空白太大了。希旺官網改進?;蛘呤俏以O置的不對 cellStyle.Indention = 0;//上面基本都是設共公的設置 //下面列出了常用的字段類型 switch (str){case stylexls.頭:// cellStyle.FillPattern = FillPatternType.LEAST_DOTS; cellStyle.SetFont(font12);break;case stylexls.時間:IDataFormat datastyle = wb.CreateDataFormat();cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");cellStyle.SetFont(font);break;case stylexls.數字:cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");cellStyle.SetFont(font);break;case stylexls.錢:IDataFormat format = wb.CreateDataFormat();cellStyle.DataFormat = format.GetFormat("¥#,##0");cellStyle.SetFont(font);break;case stylexls.url:fontcolorblue.Underline = 1;cellStyle.SetFont(fontcolorblue);break;case stylexls.百分比:cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");cellStyle.SetFont(font);break;case stylexls.中文大寫:IDataFormat format1 = wb.CreateDataFormat();cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");cellStyle.SetFont(font);break;case stylexls.科學計數法:cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");cellStyle.SetFont(font);break;case stylexls.默認:cellStyle.SetFont(font);break;}return cellStyle;}#endregion

?


提示:1.以上使用npoi版本為1.2.5版本,版本目前屬于最高版本,跟以前版本的使用是有些差別的。

????????? 2.使用以上代碼,需要添加兩個npoi的dll。Ionic.Zip.dll,NPOI.dll

?

錯誤提示:The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

錯誤原因:

  • HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,擴展名是.xls?
  • XSSFWorkbook:是操作Excel2007的版本,擴展名是.xlsx

解決方案:(引用npoi2.0)

ISheet sheet;FileStream fs = null;try{fs = new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read); HSSFWorkbook wk = new HSSFWorkbook(fs);sheet = wk.GetSheet(sheetName);}catch{fs = new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read);XSSFWorkbook wk = new XSSFWorkbook(fs);sheet = wk.GetSheet(sheetName);}finally{fs.Close();fs.Dispose();}if (sheet.LastRowNum < 1){MessageBox.Show("表內容不能為空");return;}for (int j = 1; j <= sheet.LastRowNum; j++)

注:本文轉載于:http://www.cnblogs.com/knowledgesea/archive/2012/11/16/2772547.html

?

轉載于:https://www.cnblogs.com/GISQZC/p/5420369.html

總結

以上是生活随笔為你收集整理的NPOI操作Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。