NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel
转载:http://yuncode.net/code/c_531e679b3896495
using System.Collections.Generic; |
using System.ComponentModel; |
using System.Windows.Forms; |
using System.Data.SqlClient; |
using NPOI.HSSF.UserModel; |
using NPOI.POIFS.FileSystem; |
using NPOI.HSSF.Record.CF; |
public partial class Form1 : Form |
static ICellStyle Getcellstyle(IWorkbook wb, stylexls str) |
ICellStyle cellStyle = wb.CreateCellStyle(); |
//也可以一种字体,写一些公共属性,然后在下面需要时加特殊的 |
IFont font12 = wb.CreateFont(); |
font12.FontHeightInPoints = 10; |
font12.FontName = "微软雅黑"; |
IFont font = wb.CreateFont(); |
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.MAROON.index; |
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT; |
cellStyle.VerticalAlignment = VerticalAlignment.CENTER; |
cellStyle.WrapText = true; |
//缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对 |
// cellStyle.FillPattern = FillPatternType.LEAST_DOTS; |
cellStyle.SetFont(font12); |
IDataFormat datastyle = wb.CreateDataFormat(); |
cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd"); |
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); |
IDataFormat format = wb.CreateDataFormat(); |
cellStyle.DataFormat = format.GetFormat("¥#,##0"); |
fontcolorblue.Underline = 1; |
cellStyle.SetFont(fontcolorblue); |
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%"); |
IDataFormat format1 = wb.CreateDataFormat(); |
cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0"); |
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00"); |
private void btnwrite_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); |
sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3)); |
IRow row0 = sh.CreateRow(0); |
ICell icell1top0 = row0.CreateCell(0); |
ICell icell1top1 = row0.CreateCell(1); |
ICell icell1top2 = row0.CreateCell(2); |
ICell icell1top3 = row0.CreateCell(3); |
icell1top0.CellStyle = Getcellstyle(wb, stylexls.头); |
icell1top0.SetCellValue("标题合并单元"); |
IRow row1 = sh.CreateRow(1); |
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("百度收录"); |
string sql = "select top 100 urlnam,url,bdtim,bdsl from zhiyuan"; |
using (SqlDataReader dr = SqlHelper.ExecuteReaderText(sql, null)) |
IRow row = sh.CreateRow(i); |
ICell icell = row.CreateCell(0); |
icell.CellStyle = Getcellstyle(wb, stylexls.默认); |
icell.SetCellValue(dr.GetValue(0).ToString()); |
ICell icell1 = row.CreateCell(1); |
icell1.CellStyle = Getcellstyle(wb, stylexls.url); |
icell1.SetCellValue(dr.GetValue(1).ToString()); |
HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.URL); |
link.Address = (dr.GetValue(1).ToString()); |
icell1.Hyperlink = (link); |
ICell icell2 = row.CreateCell(2); |
icell2.CellStyle = Getcellstyle(wb, stylexls.时间); |
icell2.SetCellValue(dr.IsDBNull(2) ? Convert.ToDateTime("1990-1-1") : dr.GetDateTime(2)); |
ICell icell3 = row.CreateCell(3); |
icell3.CellStyle = Getcellstyle(wb, stylexls.默认); |
icell3.SetCellValue(dr.IsDBNull(3) ? 0 : dr.GetInt32(3)); |
using (FileStream fs = File.OpenWrite("xxx.xls")) |
MessageBox.Show("Excel已经写入成功!"); |
private void CreateRow(IRow row, int j, SqlDataReader dr, ICellStyle cellstyle) |
if (dr.GetFieldType(j).Name == "Int32") |
row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? 0 : dr.GetInt32(j)); |
else if (dr.GetFieldType(j).Name == "Int16") |
{ row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? 0 : dr.GetInt16(j)); } |
else if (dr.GetFieldType(j).Name == "Int64") |
{ row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? 0 : dr.GetInt64(j)); } |
else if (dr.GetFieldType(j).Name == "String") |
{ row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? "" : dr.GetString(j)); } |
else if (dr.GetFieldType(j).Name == "DateTime") |
ICell cell = row.CreateCell(j); |
cell.CellStyle = cellstyle; |
cell.SetCellValue(dr.IsDBNull(j) ? Convert.ToDateTime("1990-1-1") : dr.GetDateTime(j)); |
else if (dr.GetFieldType(j).Name == "Double") |
{ row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? 0 : dr.GetDouble(j)); } |
else if (dr.GetFieldType(j).Name == "Byte[]") |
{ row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? 0 : dr.GetByte(j)); } |
else if (dr.GetFieldType(j).Name == "Decimal") |
{ row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? 0 : dr.GetDouble(j)); } |
row.CreateCell(j).SetCellValue(dr.IsDBNull(j) ? "" : dr.GetValue(j).ToString()); |
private void btnreade_Click(object sender, EventArgs e) |
if (DialogResult.OK == openFileDialog1.ShowDialog()) |
using (FileStream fs = File.OpenRead(openFileDialog1.FileName)) |
IWorkbook wk = new HSSFWorkbook(fs); |
for (int i = 0; i < wk.NumberOfSheets; i++) |
ISheet wk1 = wk.GetSheetAt(i); |
txtout.AppendText("====================" + wk1.SheetName + "================\r\n"); |
for (int j = 0; j < wk1.LastRowNum + 1; j++) |
IRow row = wk1.GetRow(j); |
for (int k = 0; k < row.LastCellNum + 1; k++) |
txtout.AppendText(string.Format("{0}\t", row.GetCell(k) == null ? "" : row.GetCell(k).ToString())); |
txtout.AppendText("\r\n"); |
private void button1_Click(object sender, EventArgs e) |
if (DialogResult.OK == openFileDialog1.ShowDialog()) |
string str = htmlxsl.Gethtmlxls(openFileDialog1.FileName); |
using (FileStream fs = File.OpenWrite("1.htm")) |
byte[] b = Encoding.Default.GetBytes(str); |
fs.Write(b, 0, b.Length); |
using NPOI.HSSF.UserModel; |
using NPOI.POIFS.FileSystem; |
using NPOI.HSSF.Record.CF; |
public static class htmlxsl |
private static ISheet sht; |
public static string Gethtmlxls(string path) |
IWorkbook wb = new HSSFWorkbook(new FileStream(path, FileMode.Open)); |
sht = wb.GetSheet("zhiyuan"); |
int rowsCount = sht.LastRowNum; |
//为保证Table布局与Excel一样,这里应该取所有行中的最大列数(需要遍历整个Sheet)。 |
//为少一交全Excel遍历,提高性能,我们可以人为把第0行的列数调整至所有行中的最大列数。 |
int colsCount = sht.GetRow(0).LastCellNum; |
StringBuilder table = new StringBuilder(rowsCount * 32); |
table.Append("<table border=‘1px‘>"); |
for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++) |
for (int colIndex = 0; colIndex < colsCount; colIndex++) |
GetTdMergedInfo(rowIndex, colIndex, out colSpan, out rowSpan, out isByRowMerged); |
//注意被合并的行或列不输出的处理方式不一样,见下面一处的注释说明了列合并后不输出TD的处理方式。 |
table.Append(string.Format(" colSpan={0}", colSpan)); |
table.Append(string.Format(" rowSpan={0}", rowSpan)); |
table.Append(sht.GetRow(rowIndex).GetCell(colIndex)); |
//列被合并之后此行将少输出colSpan-1个TD。 |
table.Append("</table>"); |
/// 获取Table某个TD合并的列数和行数等信息。与Excel中对应Cell的合并行数和列数一致。 |
/// <param name="rowIndex">行号</param> |
/// <param name="colIndex">列号</param> |
/// <param name="colspan">TD中需要合并的行数</param> |
/// <param name="rowspan">TD中需要合并的列数</param> |
/// <param name="rowspan">此单元格是否被某个行合并包含在内。如果被包含在内,将不输出TD。</param> |
private static void GetTdMergedInfo(int rowIndex, int colIndex, out int colspan, out int rowspan,out bool isByRowMerged) |
int regionsCuont = sht.NumMergedRegions; |
NPOI.SS.Util.CellRangeAddress region; |
for (int i = 0; i < regionsCuont; i++) |
region = sht.GetMergedRegion(i); |
if (region.FirstRow == rowIndex && region.FirstColumn == colIndex) |
colspan = region.LastColumn - region.FirstColumn + 1; |
rowspan = region.LastRow - region.FirstRow + 1; |
else if (rowIndex > region.FirstRow && rowIndex <= region.LastRow && colIndex >= region.FirstColumn && colIndex <= region.LastColumn) |
NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel
原文:http://www.cnblogs.com/MyKingDragon/p/4198478.html