使用Npoi操作excel的解決辦法
Npoi 簡(jiǎn)介
--------------------------------------------------------------------------------
1.整個(gè)Excel表格叫做工作表:WorkBook(工作薄),包含的叫頁(yè)(工作表):Sheet;行:Row;單元格Cell。
2.Npoi 下載地址:http://npoi.codeplex.com/releases/view/38113
4.忘了告訴大家npoi是做什么的了,npoi 能夠讀寫幾乎所有的Office 97-2003文件格式,至少能夠支持Word, PowerPoint, Excel, Visio的格式。
--------------------------------------------------------------------------------
使用Npoi創(chuàng)建一個(gè)簡(jiǎn)單的xls文件
--------------------------------------------------------------------------------
//創(chuàng)建xls文件
private void button1_Click(object sender, EventArgs e)
{
//創(chuàng)建工作薄
HSSFWorkbook wk = new HSSFWorkbook();
//創(chuàng)建一個(gè)名稱為mySheet的表
ISheet tb = wk.CreateSheet("mySheet");
//創(chuàng)建一行,此行為第二行
IRow row = tb.CreateRow(1);
for (int i = 0; i < 20; i++)
{
ICell cell = row.CreateCell(i); //在第二行中創(chuàng)建單元格
cell.SetCellValue(i);//循環(huán)往第二行的單元格中添加數(shù)據(jù)
}
using (FileStream fs = File.OpenWrite(@"c:/myxls.xls")) //打開(kāi)一個(gè)xls文件,如果沒(méi)有則自行創(chuàng)建,如果存在myxls.xls文件則在創(chuàng)建是不要打開(kāi)該文件!
{
wk.Write(fs); //向打開(kāi)的這個(gè)xls文件中寫入mySheet表并保存。
MessageBox.Show("提示:創(chuàng)建成功!");
}
}
使用Npoi讀取一個(gè)簡(jiǎn)單的xls文件
--------------------------------------------------------------------------------
//讀取xls文件
private void button2_Click(object sender, EventArgs e)
{ StringBuilder sbr = new StringBuilder();
using (FileStream fs = File.OpenRead(@"c:/myxls.xls")) //打開(kāi)myxls.xls文件
{
HSSFWorkbook wk = new HSSFWorkbook(fs); //把xls文件中的數(shù)據(jù)寫入wk中
for (int i = 0; i < wk.NumberOfSheets; i++) //NumberOfSheets是myxls.xls中總共的表數(shù)
{
ISheet sheet = wk.GetSheetAt(i); //讀取當(dāng)前表數(shù)據(jù)
for (int j = 0; j <= sheet.LastRowNum; j++) //LastRowNum 是當(dāng)前表的總行數(shù)
{
IRow row = sheet.GetRow(j); //讀取當(dāng)前行數(shù)據(jù)
if (row != null)
{
sbr.Append("-------------------------------------\r\n"); //讀取行與行之間的提示界限
for (int k = 0; k <= row.LastCellNum; k++) //LastCellNum 是當(dāng)前行的總列數(shù)
{
ICell cell = row.GetCell(k); //當(dāng)前表格
if (cell != null)
{
sbr.Append(cell.ToString()); //獲取表格中的數(shù)據(jù)并轉(zhuǎn)換為字符串類型
}
}
}
}
}
}
sbr.ToString();
using (StreamWriter wr = new StreamWriter(new FileStream(@"c:/myText.txt", FileMode.Append))) //把讀取xls文件的數(shù)據(jù)寫入myText.txt文件中
{
wr.Write(sbr.ToString());
wr.Flush();
}
}
--------------------------------------------------------------------------------
使用Npoi創(chuàng)建一個(gè)常用的xls文件
--------------------------------------------------------------------------------
//創(chuàng)建一個(gè)常用的xls文件
private void button3_Click(object sender, EventArgs e)
{
IWorkbook wb = new HSSFWorkbook();
//創(chuàng)建表
ISheet sh = wb.CreateSheet("zhiyuan");
//設(shè)置單元的寬度
sh.SetColumnWidth(0, 15 * 256);
sh.SetColumnWidth(1, 35 * 256);
sh.SetColumnWidth(2, 15 * 256);
sh.SetColumnWidth(3, 10 * 256);
int i = 0;
#region 練習(xí)合并單元格
sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3));
//CellRangeAddress()該方法的參數(shù)次序是:開(kāi)始行號(hào),結(jié)束行號(hào),開(kāi)始列號(hào),結(jié)束列號(hào)。
IRow row0 = sh.CreateRow(0);
row0.Height = 20 * 20;
ICell icell1top0 = row0.CreateCell(0);
icell1top0.CellStyle = Getcellstyle(wb, stylexls.頭);
icell1top0.SetCellValue("標(biāo)題合并單元");
#endregion
i++;
#region 設(shè)置表頭
IRow row1 = sh.CreateRow(1);
row1.Height = 20 * 20;
ICell icell1top = row1.CreateCell(0);
icell1top.CellStyle = Getcellstyle(wb, stylexls.頭);
icell1top.SetCellValue("網(wǎng)站名");
ICell icell2top = row1.CreateCell(1);
icell2top.CellStyle = Getcellstyle(wb, stylexls.頭);
icell2top.SetCellValue("網(wǎng)址");
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("提示:創(chuàng)建成功!");
}
}
#region 定義單元格常用到樣式的枚舉
public enum stylexls
{
頭,
url,
時(shí)間,
數(shù)字,
錢,
百分比,
中文大寫,
科學(xué)計(jì)數(shù)法,
默認(rèn)
}
#endregion
#region 定義單元格常用到樣式
static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
{
ICellStyle cellStyle = wb.CreateCellStyle();
//定義幾種字體
//也可以一種字體,寫一些公共屬性,然后在下面需要時(shí)加特殊的
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;
//背景圖形,我沒(méi)有用到過(guò)。感覺(jué)很丑
//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;
//水平對(duì)齊
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
//垂直對(duì)齊
cellStyle.VerticalAlignment = VerticalAlignment.CENTER;
//自動(dòng)換行
cellStyle.WrapText = true;
//縮進(jìn);當(dāng)設(shè)置為1時(shí),前面留的空白太大了。希旺官網(wǎng)改進(jìn)?;蛘呤俏以O(shè)置的不對(duì)
cellStyle.Indention = 0;
//上面基本都是設(shè)共公的設(shè)置
//下面列出了常用的字段類型
switch (str)
{
case stylexls.頭:
// cellStyle.FillPattern = FillPatternType.LEAST_DOTS;
cellStyle.SetFont(font12);
break;
case stylexls.時(shí)間:
IDataFormat datastyle = wb.CreateDataFormat();
cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
cellStyle.SetFont(font);
break;
case stylexls.數(shù)字:
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.科學(xué)計(jì)數(shù)法:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
cellStyle.SetFont(font);
break;
case stylexls.默認(rèn):
cellStyle.SetFont(font);
break;
}
return cellStyle;
}
#endregion
--------------------------------------------------------------------------------
提示:1.以上使用npoi版本為1.2.5版本,版本目前屬于最高版本,跟以前版本的使用是有些差別的。
2.使用以上代碼,需要添加兩個(gè)npoi的dll。Ionic.Zip.dll,NPOI.dll
- 關(guān)于Poi讀取Excel引發(fā)內(nèi)存溢出問(wèn)題的解決方法
- java 中 poi解析Excel文件版本問(wèn)題解決辦法
- Java 使用poi把數(shù)據(jù)庫(kù)中數(shù)據(jù)導(dǎo)入Excel的解決方法
- 解決PHP 7編譯安裝錯(cuò)誤:cannot stat ‘phar.phar’: No such file or directory
- Laravel執(zhí)行migrate命令提示:No such file or directory的解決方法
- Linux執(zhí)行.sh文件時(shí)提示No such file or directory該怎么辦(三種解決辦法)
- bash: /usr/bin/autocrorder: /usr/bin/python^M: bad interpreter: No such file or directory
- CodeIgniter錯(cuò)誤mysql_connect(): No such file or directory解決方法
- shopex主機(jī)報(bào)錯(cuò)誤請(qǐng)求解決方案(No such file or directory)
- POI導(dǎo)出Excel報(bào)錯(cuò)No such file or directory的解決方法
相關(guān)文章
C#難點(diǎn)逐個(gè)擊破(2):out返回參數(shù)
之前提到ref是將原方法中的參數(shù)影響的結(jié)果返回到調(diào)用它的方法中,out與ref類似,相比之下,ref傳遞參數(shù)的地址,out是返回值。2010-02-02C#實(shí)現(xiàn)DataList里面嵌套DataList的折疊菜單
這篇文章主要介紹了C#實(shí)現(xiàn)DataList里面嵌套DataList的折疊菜單,以實(shí)例形式詳細(xì)分析了DataList嵌套實(shí)現(xiàn)折疊菜單所涉及的JavaScript、HTML與C#相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11使用C#創(chuàng)建Windows服務(wù)的實(shí)例代碼
這篇文章主要介紹了使用C#創(chuàng)建Windows服務(wù)的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07c# NPOI 如何在指定單元格導(dǎo)入導(dǎo)出圖片
這篇文章主要介紹了c# NPOI 如何在指定單元格導(dǎo)入導(dǎo)出圖片,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03