excel模板设置

私车公用单  
 
 
申请信息  
申请单号: $header.ORDER_NO 申请日期: $header.CREATE_TIME $auotheight
申请部门: $header.APPLY_DEPT 申请人: $header.APPLY_USER $auotheight
项目: $header.PROJECT_RD_NAME 客户: $header.CUST_NAME $auotheight
出差单号: $header.BST_NO     $auotheight
用车信息  
派车日期 使用人 车辆类型车牌 用车事由 出发地  目的地 行驶路线  
$list.detail ASSIGN_DATE $CAR_OWNER $CAR_NUMBER $REASON $START_POINT $END_POINT $listend.ROUTE_NOTES $auotheight
签核信息  
序号 日期 步骤 签核部门 签核人 结果 签核意见  
$list.ad SEQ $EVENT_TIME $AUDIT_DESC $DEPT_NAME $USER_NAME $AUDIT_RESULT $listend.AUDIT_MARK $auotheight
                   

使用方式:

1.首先查询数据,DataSet ds=GetData(orderno);

2.然后调用,支持一个sheet模板导出多个数据sheet,多个sheet模板导出多个数据sheet

 string fpath =  "私车公用单.xls";ExcelHelper.Helper excel = new ExcelHelper.Helper();NPOI241.SS.UserModel.IWorkbook workbook = excel.Export(fpath, (book) =>{List<ExcelHelper.Helper.ExcelHelperSheetInfo> sinfo = new List<ExcelHelper.Helper.ExcelHelperSheetInfo>();ExcelHelper.Helper.ExcelHelperSheetInfo info = new ExcelHelper.Helper.ExcelHelperSheetInfo();sinfo.Add(info);info.ModelSheet = book.GetSheetAt(0);info.NewSheetData = new List<object>();Hashtable ht = new Hashtable();info.NewSheetData.Add( ht);ht.Add("header", ds.Tables[0].Rows[0]);ht.Add("detail", ds.Tables[1].DefaultView);ht.Add("ad",ds.Tables[2].DefaultView);return sinfo;}, (book, sheet, info) => {sheet.ProtectSheet("123456");});
using Newtonsoft.Json;
using NPOI241.HSSF.Record;
using NPOI241.HSSF.UserModel;
using NPOI241.HSSF.Util;
using NPOI241.SS.UserModel;
using NPOI241.SS.UserModel.Charts;
using NPOI241.SS.Util;
using NPOI241.XSSF.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;namespace WebPortal.Common.ExcelHelper
{public struct SimpleCell{/// <summary>/// 文本/// </summary>public string T { get; set; }/// <summary>/// colspan/// </summary>public int C { get; set; }/// <summary>/// rowspan/// </summary>public int R { get; set; }/// <summary>/// 样式 文本颜色,背景颜色,字体是否加粗,对齐,宽度/// </summary>public string S { get; set; }/// <summary>/// 备注/// </summary>public string RK { get; set; }}public struct SimpleChart{/// <summary>/// 标题/// </summary>public string Title { get; set; }/// <summary>/// 类型 chart类型  bar和line/// </summary>public string Type { get; set; }/// <summary>/// 数据起始单元格/// </summary>public int Col1 { get; set; }/// <summary>/// 数据起始行/// </summary>public int Row1 { get; set; }/// <summary>/// 数据结束单元格/// </summary>public int Col2 { get; set; }/// <summary>/// 数据结束行/// </summary>public int Row2 { get; set; }/// <summary>/// 是否应用右边Y轴,默认左边Y轴/// </summary>public bool RightY { get; set; }}public class SimpleTable{/// <summary>/// 行数据如/// </summary>public List<List<SimpleCell>> Rows { get; set; }/// <summary>/// 颜色/// </summary>public List<string> Colors { get; set; }/// <summary>/// 样式/// </summary>public List<string> Styles { get; set; }/// <summary>/// 冻结的行 只考虑冻结表头/// </summary>public int FRow { get; set; }/// <summary>/// 冻结的列/// </summary>public int FCol { get; set; }/// <summary>/// 图表信息/// </summary>public List<SimpleChart> Chart { get; set; }/// <summary>/// X轴数据起始单元格/// </summary>public int Col1 { get; set; }/// <summary>/// X轴数据起始行/// </summary>public int Row1 { get; set; }/// <summary>/// X轴数据结束单元格/// </summary>public int Col2 { get; set; }/// <summary>/// X轴数据结束行/// </summary>public int Row2 { get; set; }/// <summary>/// 是否设置右边Y轴/// </summary>public bool RightY { get; set; }/// <summary>/// 是否只读/// </summary>public bool IsReadOnly { get; set; }public string PWD { get; set; }}public struct CellStyleWrap{public int? Width { get; set; }public ICellStyle CellStyle { get; set; }public CellStyleWrap(int? w, ICellStyle cs): this(){this.Width = w;this.CellStyle = cs;}}public class Helper{void CreateChart(ISheet sheet, int col1, int col2, int row1, int row2, SimpleTable st){IDrawing drawing = sheet.CreateDrawingPatriarch();IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, col1, row1, col2, row2);var chart = drawing.CreateChart(anchor) as XSSFChart;//生成图例var legend = chart.GetOrCreateLegend();//图例位置legend.Position = LegendPosition.Top;// X轴.var bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);//Y轴IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);leftAxis.Crosses = (AxisCrosses.AutoZero);//暂时不支持双y轴IValueAxis rigthAxis = leftAxis;//if (st.RightY)//{//    rigthAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Right);//    rigthAxis.Crosses = (AxisCrosses.AutoZero);//}//x轴IChartDataSource<double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(st.Row1, st.Row2, st.Col1, st.Col2));IBarChartData<double, double> bardata = null;ILineChartData<double, double> linedata = null;IScatterChartData<double, double> scatterdata = null;foreach (SimpleChart sc in st.Chart){//图表var data = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(sc.Row1, sc.Row2, sc.Col1, sc.Col1));SetDoubleData(sheet, sc.Col1, sc.Col2, sc.Row1, sc.Row2);switch (sc.Type){case "line":if (linedata == null)linedata = chart.ChartDataFactory.CreateLineChartData<double, double>();var sline = linedata.AddSeries(xs, data);sline.SetTitle(sc.Title);break;case "bar":if (bardata == null)bardata = chart.ChartDataFactory.CreateBarChartData<double, double>();var sbar = bardata.AddSeries(xs, data);sbar.SetTitle(sc.Title);break;case "scatter":if (scatterdata == null)scatterdata = chart.ChartDataFactory.CreateScatterChartData<double, double>();var stline = scatterdata.AddSeries(xs, data);stline.SetTitle(sc.Title);break;default:throw new Exception("暂不支持【" + sc.Type + "】类型的图表,只支持line、bar、scatter!");}}if (bardata != null)chart.Plot(bardata, bottomAxis, leftAxis);if (linedata != null)chart.Plot(linedata, bottomAxis, leftAxis);if (scatterdata != null)chart.Plot(scatterdata, bottomAxis, leftAxis);}public void SetDoubleData(ISheet sheet, int col1, int col2, int row1, int row2){while (row1 <= row2){IRow row = sheet.GetRow(row1);if (row == null)continue;int begcol = col1;while (begcol <= col2){ICell cell = row.GetCell(begcol);if (cell.CellType == CellType.String){var value = cell.StringCellValue;if (!string.IsNullOrWhiteSpace(value)){value = value.Replace(",", "").Trim();double dbval;if (double.TryParse(value, out dbval)){cell.SetCellType(CellType.Numeric);cell.SetCellValue(dbval);}}}begcol += 1;}row1 += 1;}}private void aa(List<short> listcolor, short color){if (color > PaletteRecord.STANDARD_PALETTE_SIZE)return;if (listcolor.IndexOf(color) < 0){listcolor.Add(color);}}private Dictionary<string, short> CreateColor(HSSFWorkbook hssfWorkbook, List<string> colors){Dictionary<string, short> dictret = new Dictionary<string, short>();HSSFPalette palette = hssfWorkbook.GetCustomPalette();List<short> listcolor = new List<short>();short index = 0;IFont existsfont;while (index < hssfWorkbook.NumberOfFonts){existsfont = hssfWorkbook.GetFontAt(index);aa(listcolor, existsfont.Color);index += 1;}index = 0;ICellStyle existscs;while (index < hssfWorkbook.NumCellStyles){existscs = hssfWorkbook.GetCellStyleAt(index);aa(listcolor, existscs.FillBackgroundColor);aa(listcolor, existscs.FillForegroundColor);aa(listcolor, existscs.BottomBorderColor);aa(listcolor, existscs.TopBorderColor);aa(listcolor, existscs.LeftBorderColor);aa(listcolor, existscs.RightBorderColor);index += 1;}for (int i = 0; i < colors.Count; i++){string color = colors[i];string[] colorstr = color.Replace("rgba(", "").Replace("rgb(", "").Replace(")", "").Split(',');byte r = Convert.ToByte(colorstr[0].Trim()), g = Convert.ToByte(colorstr[1].Trim()), b = Convert.ToByte(colorstr[2].Trim());HSSFColor hc = palette.FindColor(r, g, b);if (hc != null){dictret.Add(color, hc.Indexed);listcolor.Add(hc.Indexed);continue;}short colorindex = PaletteRecord.FIRST_COLOR_INDEX;short endcolorindex = PaletteRecord.STANDARD_PALETTE_SIZE;while (colorindex < endcolorindex){if (listcolor.IndexOf(colorindex) < 0){palette.SetColorAtIndex(colorindex, r, g, b);dictret.Add(color, colorindex);listcolor.Add(colorindex);break;}colorindex += 1;}}return dictret;}private Dictionary<string, XSSFColor> CreateColor(XSSFWorkbook hssfWorkbook, List<string> colors){Dictionary<string, XSSFColor> dictret = new Dictionary<string, XSSFColor>();List<short> listcolor = new List<short>();for (int i = 0; i < colors.Count; i++){string color = colors[i];string[] colorstr = color.Replace("rgba(", "").Replace("rgb(", "").Replace(")", "").Split(',');byte r = Convert.ToByte(colorstr[0].Trim()), g = Convert.ToByte(colorstr[1].Trim()), b = Convert.ToByte(colorstr[2].Trim());dictret.Add(color, new XSSFColor(new byte[] { r, g, b }));}return dictret;}private Dictionary<string, CellStyleWrap> CreateCellStyle(IWorkbook book, ICellStyle df, List<string> style, Dictionary<string, short> diccolor){Dictionary<string, CellStyleWrap> dicret = new Dictionary<string, CellStyleWrap>();foreach (string str in style){string[] strs = str.Split(';');string fc = strs[0];string bc = strs[1];string fw = strs[2];string al = strs[3];string w = strs[4];if (fw == "bold")fw = "400";ICellStyle cs;if (fc != "" || bc != "" || fw != "" || al != ""){cs = book.CreateCellStyle();cs.CloneStyleFrom(df);}else{cs = df;}short color;if (bc != "" && diccolor.TryGetValue(bc, out color)){cs.FillForegroundColor = color;cs.FillPattern = FillPattern.SolidForeground;}color = 0;short fontcolor = 0;if (fw != "")short.TryParse(fw, out color);if (fc != "")diccolor.TryGetValue(fc, out fontcolor);if (color > 0 || fontcolor > 0){color *= 2;IFont font = cs.GetFont(book);IFont fontnew = book.CreateFont();fontnew.Charset = font.Charset;fontnew.Color = fontcolor == 0 ? font.Color : fontcolor;fontnew.FontHeight = font.FontHeight;fontnew.FontHeightInPoints = font.FontHeightInPoints;fontnew.FontName = font.FontName;fontnew.IsItalic = font.IsItalic;fontnew.IsStrikeout = font.IsStrikeout;fontnew.TypeOffset = font.TypeOffset;fontnew.Underline = font.Underline;fontnew.Boldweight = color == 0 ? font.Boldweight : color;cs.SetFont(fontnew);}switch (al.ToLower()){case "center":cs.Alignment = HorizontalAlignment.Center;break;case "right":cs.Alignment = HorizontalAlignment.Right;break;}int? with = null;int widthtmp;if (int.TryParse(w, out widthtmp))with = widthtmp;dicret.Add(str, new CellStyleWrap(with, cs));}return dicret;}private Dictionary<string, CellStyleWrap> CreateCellStyle(IWorkbook book, ICellStyle df, List<string> style, Dictionary<string, XSSFColor> diccolor){Dictionary<string, CellStyleWrap> dicret = new Dictionary<string, CellStyleWrap>();foreach (string str in style){string[] strs = str.Split(';');string fc = strs[0];string bc = strs[1];string fw = strs[2];string al = strs[3];string w = strs[4];if (fw == "bold")fw = "400";ICellStyle cs;if (fc != "" || bc != "" || fw != "" || al != ""){cs = book.CreateCellStyle();cs.CloneStyleFrom(df);}else{cs = df;}short color;XSSFColor bcolor;if (bc != "" && diccolor.TryGetValue(bc, out bcolor)){((XSSFCellStyle)cs).SetFillForegroundColor(bcolor);cs.FillPattern = FillPattern.SolidForeground;}color = 0;XSSFColor fontcolor = null;bool hasfc = false;if (fw != "")short.TryParse(fw, out color);if (fc != "")hasfc = diccolor.TryGetValue(fc, out fontcolor);if (color > 0 || hasfc){color *= 2;IFont font = cs.GetFont(book);IFont fontnew = book.CreateFont();fontnew.Charset = font.Charset;if (!hasfc)fontnew.Color = font.Color;else((XSSFFont)fontnew).SetColor(fontcolor);fontnew.FontHeight = font.FontHeight;fontnew.FontHeightInPoints = font.FontHeightInPoints;fontnew.FontName = font.FontName;fontnew.IsItalic = font.IsItalic;fontnew.IsStrikeout = font.IsStrikeout;fontnew.TypeOffset = font.TypeOffset;fontnew.Underline = font.Underline;fontnew.Boldweight = color == 0 ? font.Boldweight : color;cs.SetFont(fontnew);}switch (al.ToLower()){case "center":cs.Alignment = HorizontalAlignment.Center;break;case "right":cs.Alignment = HorizontalAlignment.Right;break;}int? with = null;int widthtmp;if (int.TryParse(w, out widthtmp))with = widthtmp;dicret.Add(str, new CellStyleWrap(with, cs));}return dicret;}public void ToExcel(string data, Stream stream, bool is2003){try{SimpleTable st = JsonConvert.DeserializeObject<SimpleTable>(data);IWorkbook book = null;if (is2003){book = new HSSFWorkbook();}else{book = new XSSFWorkbook();}ICellStyle dfcs = book.CreateCellStyle();dfcs.Alignment = HorizontalAlignment.Left;dfcs.VerticalAlignment = VerticalAlignment.Center;dfcs.BorderBottom = BorderStyle.Thin;dfcs.BorderLeft = BorderStyle.Thin;dfcs.BorderRight = BorderStyle.Thin;dfcs.BorderTop = BorderStyle.Thin;if (st.IsReadOnly)dfcs.IsLocked = true;Dictionary<string, CellStyleWrap> dicstyle = null;if (is2003){Dictionary<string, short> dicColor = CreateColor((HSSFWorkbook)book, st.Colors);dicstyle = CreateCellStyle(book, dfcs, st.Styles, dicColor);}else{Dictionary<string, XSSFColor> dicColor = CreateColor((XSSFWorkbook)book, st.Colors);dicstyle = CreateCellStyle(book, dfcs, st.Styles, dicColor);}ISheet sheet = book.CreateSheet();IDrawing patr = sheet.CreateDrawingPatriarch();int rowindex = 0;foreach (List<SimpleCell> cells in st.Rows){IRow row = sheet.CreateRow(rowindex);int cellindex = 0;foreach (SimpleCell sc in cells){ICell cell = row.CreateCell(cellindex);if (!string.IsNullOrEmpty(sc.T)){cell.SetCellValue(sc.T);}CellStyleWrap csw = default(CellStyleWrap);if (sc.S != null && dicstyle.TryGetValue(sc.S, out csw)){cell.CellStyle = csw.CellStyle;}else{cell.CellStyle = dfcs;}if (!string.IsNullOrEmpty(sc.RK)){IComment comment = null;if (is2003){comment = patr.CreateCellComment(new HSSFClientAnchor(255, 125, 1023, 150, cellindex, rowindex, cellindex + 2, rowindex + 4));comment.String = new HSSFRichTextString(sc.RK);}else{comment = patr.CreateCellComment(new XSSFClientAnchor(255, 125, 1023, 150, cellindex, rowindex, cellindex + 2, rowindex + 4));comment.String = new XSSFRichTextString(sc.RK);}cell.CellComment = comment;}if (sc.C > 0 || sc.R > 0){sheet.AddMergedRegion(new NPOI241.SS.Util.CellRangeAddress(rowindex, rowindex + (sc.R == 0 ? 0 : sc.R - 1), cellindex, cellindex + (sc.C == 0 ? 0 : sc.C - 1)));}cellindex += 1;}rowindex += 1;}int lastrowindex = rowindex;List<SimpleCell> rows = st.Rows[st.FRow];int index = 0;foreach (SimpleCell sc in rows){CellStyleWrap csw = default(CellStyleWrap);if (sc.S != null && dicstyle.TryGetValue(sc.S, out csw) && csw.Width != null){sheet.SetColumnWidth(index, csw.Width.Value * 256);}index += 1;}if (st.FCol > 0 || st.FRow > 0){sheet.CreateFreezePane(st.FCol, st.FRow);}if (st.Chart != null && st.Chart.Count > 0){CreateChart(sheet, 0, 0 + 10, lastrowindex + 3, lastrowindex + 3 + 20, st);}if (st.IsReadOnly){sheet.ProtectSheet(st.PWD);}book.Write(stream);}catch{throw;}}public void ToExcel(Stream stream, DataTable[] dts, NameValueCollection[] titles, bool is2003){try{IWorkbook book = null;if (is2003){book = new HSSFWorkbook();}else{book = new XSSFWorkbook();}ICellStyle dfcs = book.CreateCellStyle();dfcs.Alignment = HorizontalAlignment.Left;dfcs.VerticalAlignment = VerticalAlignment.Center;dfcs.BorderBottom = BorderStyle.Thin;dfcs.BorderLeft = BorderStyle.Thin;dfcs.BorderRight = BorderStyle.Thin;dfcs.BorderTop = BorderStyle.Thin;int i = 0;foreach (DataTable dt in dts){ISheet sheet = book.CreateSheet(dt.TableName);NameValueCollection nvc = titles[i];int rowindex = 0;int cellindex = 0;IRow row;ICell cell;row = sheet.CreateRow(rowindex);foreach (string key in nvc){cell = row.CreateCell(cellindex);cell.CellStyle = dfcs;cell.SetCellValue(nvc[key]);cellindex += 1;}foreach (DataRow dr in dt.Rows){rowindex += 1;row = sheet.CreateRow(rowindex);cellindex = 0;foreach (string key in nvc){cell = row.CreateCell(cellindex);cell.CellStyle = dfcs;cell.SetCellValue(dr[key].ToString());cellindex += 1;}}i += 1;}book.Write(stream);}catch{throw;}}public class ExcelHelperSheetInfo{/// <summary>/// 新sheet数据,每一个数据项生产一个sheet页/// </summary>public List<object> NewSheetData { get; set; }/// <summary>/// 模板Sheet/// </summary>public ISheet ModelSheet { get; set; }}/// <summary>/// 绑定数据前缀,全部小写,默认$/// </summary>public string Prefix{get;set;}/// <summary>/// 列表开始前缀,,全部小写默认$list./// </summary>public string BeginListPrefix{get;set;}/// <summary>/// 列表结束前缀,全部小写,默认$listend./// </summary>public string EndListPrefix{get;set;}/// <summary>/// 自动行高前缀,全部小写 默认$auotheight,放到列后面/// </summary>public string AutoHeightPrefix{get;set;}//列表序列号字符public string ListSeqNoStr{get;set;}/// <summary>/// /// </summary>/// <param name="prefix">绑定数据前缀,默认$</param>/// <param name="beglistprefix">列表开始前缀,默认$list.</param>/// <param name="endlistprefix">列表结束前缀,默认$listend.</param>public Helper(string prefix = "$", string beglistprefix = "$list.", string endlistprefix = "$listend.", string seqnostr = "listseqno", string autoheight = "$auotheight"){this.Prefix = prefix;this.BeginListPrefix = beglistprefix;this.EndListPrefix = endlistprefix;this.ListSeqNoStr = seqnostr;this.AutoHeightPrefix = autoheight;}/// <summary>/// 检查是否是2003以前的版本,根据后缀判断/// </summary>/// <param name="file"></param>/// <returns></returns>public static bool Is2003(string file){string ext = Path.GetExtension(file);return ext.ToLower() == ".xls";}/// <summary>/// 返回正确的sheet名称/// </summary>/// <param name="sheetname"></param>/// <returns></returns>public static string GetSheetName(string sheetname){sheetname = sheetname.Replace("//", "").Replace("\"", "").Replace("[", "").Replace("]", "");sheetname = sheetname.Replace("<", "").Replace(">", "").Replace("\r", "").Replace("\n", "");sheetname = sheetname.Replace("*", "").Replace("|", "").Replace(":", "");sheetname = sheetname.Replace("?", "").Replace("/", "");return sheetname;}/// <summary>/// 按模板导出数据,保存到流/// </summary>/// <param name="modalfilepath">模板路径</param>/// <param name="dicdata">数据</param>/// <param name="fncreate">根据数据创建sheet</param>/// <param name="ms">保存到流</param>/// <param name="acend">数据填充完成后执行的方法</param>public IWorkbook Export(string modalfilepath, Func<IWorkbook, List<ExcelHelperSheetInfo>> fncreate, Action<IWorkbook, ISheet, ExcelHelperSheetInfo> acend = null){try{IWorkbook excel = null;using (FileStream fs = new FileStream(modalfilepath, FileMode.Open, FileAccess.Read, FileShare.Read)){if (Is2003(modalfilepath)){excel = new HSSFWorkbook(fs);}else{excel = new XSSFWorkbook(fs);}List<ExcelHelperSheetInfo> list = fncreate(excel);foreach (ExcelHelperSheetInfo dr in list){ISheet sheet = dr.ModelSheet;int modelsheetindex = excel.GetSheetIndex(sheet);foreach (object sheetData in dr.NewSheetData){ISheet sheetnew = excel.CloneSheet(modelsheetindex);ExprotGongYiBiao(sheetnew, sheetData);if (acend != null){acend(excel, sheetnew, dr);}}excel.RemoveSheetAt(modelsheetindex);}}return excel;}catch{throw;}}/// <summary>/// 按模板导出单个sheet/// </summary>/// <param name="sheetnew"></param>/// <param name="dicdata"></param>public void ExprotGongYiBiao(ISheet sheetnew, object dicdata){int maxrow = sheetnew.LastRowNum;int minrow = 0;int newrowno = 0;sheetnew.FitToPage = true;while (minrow <= maxrow){IRow row = sheetnew.GetRow(minrow);if (row == null){minrow += 1;continue;}int cellindex = 0;int maxcell = row.LastCellNum;bool islist = false, islistend = false;int listbegnum = 0;string listkey = "";List<string> listkeys = null;bool hassetdata = false;bool autoheight = false;while (cellindex <= maxcell){var modelcell = row.GetCell(cellindex);if (modelcell == null){cellindex += 1;continue;}string cellvalue = modelcell.ToString().Trim();string cellvaluelower = cellvalue.ToLower();if (cellvaluelower.StartsWith(BeginListPrefix)){islist = true;listbegnum = cellindex;cellvalue = cellvalue.Substring(BeginListPrefix.Length);listkey = cellvalue.Split(' ')[0];listkeys = new List<string>();listkeys.Add(cellvalue.Split(' ')[1]);}else if (islist){if (cellvaluelower.StartsWith(EndListPrefix)){islistend = true;cellvalue = cellvalue.Substring(EndListPrefix.Length);listkeys.Add(cellvalue);}else if (cellvaluelower == AutoHeightPrefix){autoheight = true;row.RemoveCell(modelcell);}else if (cellvaluelower.StartsWith(Prefix)){listkeys.Add(cellvalue.Substring(Prefix.Length));}else if (!islistend){listkeys.Add("");}}else if (cellvaluelower == AutoHeightPrefix){autoheight = true;row.RemoveCell(modelcell);}else if (cellvaluelower.StartsWith(Prefix)){cellvalue = cellvalue.Substring(Prefix.Length);SetCellValue(modelcell, cellvalue, dicdata);hassetdata = true;}cellindex += 1;}if (islist){newrowno = SetList(sheetnew, row, minrow, listkey, listkeys, listbegnum, dicdata, ListSeqNoStr, autoheight);minrow += newrowno == 0 ? 1 : newrowno;maxrow += newrowno;}else{if (hassetdata && autoheight){SetRowHeight(row);}minrow += 1;}}}/// <summary>/// 解析字符串转换成对象字符串例如Order.OrderNo,Order.Detail.ID,如果错误返回error/// </summary>/// <param name="key"></param>/// <param name="dicdata"></param>/// <returns></returns>public static object GetDataByKey(string key, object dicdata){string[] keys = key.Split('.');object ret = dicdata;try{foreach (string str in keys){if (ret == null)return null;if (ret is IDictionary){ret = (ret as IDictionary)[str];continue;}if (ret is DataRow){ret = (ret as DataRow)[str];continue;}if (ret is DataRowView){ret = (ret as DataRowView)[str];continue;}PropertyInfo pi = ret.GetType().GetProperty(str, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);if (pi == null){throw new Exception(ret.GetType().FullName + "不包含属性:" + str);}if (!pi.CanRead){throw new Exception(ret.GetType().FullName + "属性:" + str + "不可以读");}ret = pi.GetValue(ret);}}catch (Exception ex){return "Error:" + ex.Message;}return ret;}/// <summary>/// 按模板设置单元格数据/// </summary>/// <param name="cell"></param>/// <param name="key"></param>/// <param name="dicdata"></param>public static void SetCellValue(ICell cell, string key, object dicdata){object value = GetDataByKey(key, dicdata);if (value != null && value != DBNull.Value){switch (value.GetType().Name.ToLower()){case "int":case "int32":case "int16":case "short":case "byte":case "double":case "float":case "decimal":case "long":cell.SetCellValue(Convert.ToDouble(value));break;case "datetime":cell.SetCellValue(Convert.ToDateTime(value));break;default:cell.SetCellValue(value.ToString());break;}}else{cell.SetCellValue("");}}public static int SetList(ISheet sheet, IRow row, int begrowindex, string listkey, List<string> keys, int listbegcell, object dicdata, string seqnostr, bool autoheight){bool has = false;int total = 0;object listdata = GetDataByKey(listkey, dicdata);if (listdata == null || listdata == DBNull.Value)goto lblend;if (!(listdata is IEnumerable))goto lblend;IEnumerator aa = (listdata as IEnumerable).GetEnumerator();aa.Reset();ICell cellnew;int newrowindex = begrowindex;for (int i = 0; aa.MoveNext(); i++){total += 1;IRow newrow;if (i == 0){newrow = row;}else{newrow = sheet.CopyRow(begrowindex, newrowindex);newrow.Height = row.Height;}newrowindex += 1;has = true;int cellindex = listbegcell;object dr = aa.Current;foreach (string str in keys){if (str == ""){cellindex += 1;continue;}cellnew = newrow.GetCell(cellindex);if (str == seqnostr){cellnew.SetCellValue(i + 1);}else{SetCellValue(cellnew, str, dr);}cellindex += 1;}if (autoheight)SetRowHeight(newrow);}lblend:if (!has){int cellindex = listbegcell;foreach (string key in keys){if (key == ""){cellindex += 1;continue;}cellnew = row.GetCell(cellindex);cellnew.SetCellValue("");}row.Height = row.Sheet.DefaultRowHeight;}return total;}public static float GetCellWidth(ICell cell){if (cell == null)return 0;ISheet sheet = cell.Sheet;int mcount = sheet.NumMergedRegions;CellRangeAddress mergerange = null;for (int i = 0; i < mcount; i++){CellRangeAddress range = sheet.GetMergedRegion(i);if (range.FirstColumn <= cell.ColumnIndex&& range.LastColumn >= cell.ColumnIndex&& range.FirstRow <= cell.RowIndex&& range.LastRow >= cell.RowIndex){if (range.FirstColumn == cell.ColumnIndex && range.FirstRow == cell.RowIndex){mergerange = range;}else{break;}}}if (mergerange != null){float width = 0;for (int i = mergerange.FirstColumn; i <= mergerange.LastColumn; i++){width += sheet.GetColumnWidthInPixels(i);}return width;}return sheet.GetColumnWidthInPixels(cell.ColumnIndex);}/// <summary>/// /// </summary>/// <param name="sheet"></param>/// <param name="begrow">从0开始</param>/// <param name="begcol">从0开始</param>/// <param name="endrow">从0开始,请比真实位置多一行</param>/// <param name="endcol">从0开始,请比真实位置多一列</param>/// <param name="filepath"></param>public static void AddImg(ISheet sheet, int begrow, int begcol, int endrow, int endcol, string filepath){IWorkbook excel = sheet.Workbook;byte[] imgdata = File.ReadAllBytes(filepath);PictureType imgtype = PictureType.None;switch (Path.GetExtension(filepath).ToLower()){ case ".png":imgtype = PictureType.PNG;break;case ".jpg":imgtype = PictureType.JPEG;break;case ".gif":imgtype = PictureType.GIF;break;}int pictureIdx = excel.AddPicture(imgdata, imgtype);IDrawing patriarch = sheet.CreateDrawingPatriarch();// 插图片的位置  HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释IClientAnchor anchor = null;if (excel is HSSFWorkbook)anchor = new HSSFClientAnchor(0, 0, 0, 0, begcol, begrow, endcol, endrow);elseanchor = new XSSFClientAnchor(0, 0, 0, 0, begcol, begrow, endcol, endrow);//把图片插到相应的位置IPicture pict = (IPicture)patriarch.CreatePicture(anchor, pictureIdx);}public static void SetRowHeight(IRow row){ICell cell = null;ISheet sheet = row.Sheet;IWorkbook book = sheet.Workbook;IFont font = null;float rowheight = row.HeightInPoints;int begnum = row.FirstCellNum;StringFormat sf = new StringFormat(StringFormat.GenericDefault);sf.Trimming = StringTrimming.Word;for (; begnum <= row.LastCellNum; begnum++){cell = row.GetCell(begnum);if (cell == null)continue;string value = cell.ToString();if (cell.CellStyle != null)font = cell.CellStyle.GetFont(book);if (font == null || string.IsNullOrWhiteSpace(value))continue;float width = GetCellWidth(cell);if (width >= 0 && width <= 0)continue;Font newfont = new Font(font.FontName, (float)(font.FontHeightInPoints), GraphicsUnit.Point);SizeF maxsize = new SizeF(width, float.MaxValue);int c0, lc;//需要每次重新生成位图进行测量using (var graphics = Graphics.FromHwnd(IntPtr.Zero)){var size1 = graphics.MeasureString(value, newfont, maxsize, sf, out c0, out lc);rowheight = Math.Max(rowheight, size1.Height);//(float)( lc * Math.Max(sheet.DefaultRowHeightInPoints, font.FontHeightInPoints))}}row.HeightInPoints = rowheight;// beishu* rowheight;}}
}

c# 使用NPOI按模板导出excel相关推荐

  1. ​WeihanLi.Npoi 根据模板导出Excel

    WeihanLi.Npoi 根据模板导出Excel Intro 原来的导出方式比较适用于比较简单的导出,每一条数据在一行,数据列虽然自定义程度比较高,如果要一条数据对应多行就做不到了,于是就想支持根据 ...

  2. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

  3. springboot使用jxls导出excel___(万能通用模板)--- SpringBoot导入、导出Excel文件___SpringBoot整合EasyExcel模板导出Excel

    springboot使用jxls导出excel 实现思路: 首先在springBoot(或者SpringCloud)项目的默认templates目录放入提前定义好的Excel模板,然后在具体的导出接口 ...

  4. EasyExcel根据自定义模板导出Excel(包含图片、表格)

    使用EasyExcel根据模板导出excel,包含图片.表格 提示:其实使用EasyExcel根据模板导出一个excel并不难,难点在于指定图片的位置 文章目录 使用EasyExcel根据模板导出ex ...

  5. 使用EasyPoi利用excel模板导出excel表格下载

    前言:使用excel模板导出excel的好处在于可以事先在模板上定义颜色.格式等,适用于模板设计得比较灵活复杂的场景 一.添加jar包 <dependency><groupId> ...

  6. springboot+poi开发excel导出 加载Excel模板导出 Excel批量导出详解

    提到Excel导出功能,可能很多人都使用springmvc框架做过,笔者今天要给大家分享的是基于springBoot开发Excel复杂模板导出功能(所谓复杂模板指在模板里的特定表头里有不同的单元格合并 ...

  7. 阿里easyexcel通过模板导出excel

    easyexcel通过模板导出excel 之前使用其他方式进行excel的导出,像poi或者freemarker或者Beetl,效果都还行,但是总是有一些小问题.许多的解决思路都是:通过制作excel ...

  8. java使用模板导出Excel表格

    java使用模板导出Excel表格 文章目录 java使用模板导出Excel表格 模板示例 一.引入依赖 二.使用步骤 1.添加工具类 2.导出Excel 模板示例 一.引入依赖 <depend ...

  9. 使用EasyPoi根据模板导出Excel或word文档

    接着上篇文章 Java根据模板导出Excel并生成多个Sheet 简单介绍下EasyPoi的使用,直接上代码吧 首先当然是先引入jar包了,看下图 其次,还是贴代码吧看实例,下面是根据模板导出的工具类 ...

  10. java poi导出excel模板_POI通过模板导出EXCEL文件的实例

    一般的EXCEL导出使用POI先创建一个HSSFWorkbook,然后通过不断创建HSSFRow,HSSFCell后设置单元格内容便可以完成导出. 这次在项目中需要用到模板,导出的内容包括(1.模板中 ...

最新文章

  1. python四大软件-9个使用Python的世界级软件公司
  2. explain的讲解
  3. qfile 计算文件有多少行_肉牛不喂精料行吗?如何计算肉牛一天喂多少精料?
  4. 获取Android版本信息和电话信息
  5. Centos7下更改docker镜像和容器的默认路径
  6. 奇怪的DNS问题,虚拟机下可以,真实的环境却出错,希望大家帮助下
  7. 高通最强芯片855发布!AI性能比华为苹果翻倍,商用5G,标配屏下指纹
  8. 使用数据分析工具的注意事项
  9. 【现代软件工程】6月中旬团队项目心得
  10. php code128扫码不能识别,求助excel中code128字体打印出的条码扫描枪读不出
  11. 页面跳转 并将改页面从历史栈中删除
  12. 在上海乐字节学习CRM项目管理
  13. codeforces 592D(树DP)
  14. 洛谷P1873 [COCI 2011/2012 #5] EKO / 砍树(二分法)
  15. 打开Visual Studio Community 2017 报出“许可证已过期”
  16. AjaxPro的AJAX示例
  17. 6_3.信道与信道容量
  18. linux毁了所有数据,小心!数据在你不注意的时候居然偷偷损毁了
  19. data:image/png;base64,iVBORw0KGg... 表示的是一张图片
  20. 俞敏洪在同济大学的演讲词

热门文章

  1. 计算机网络保密管理十个严禁,涉密网络保密管理规定
  2. 10款精选的后台管理系统,收藏吧!
  3. 如何更改电脑开机密码
  4. C++学习:第三阶段(C++提高编程)————B站黑马程序员C++学习笔记
  5. b-tree的索引页总览
  6. sql优化的N种方法_持续更新
  7. 软件需求规格说明书模板
  8. 【ubantu18.04 有线网络驱动安装 r8215-9.007.01】
  9. 超级安全!Python 合成多张图片到PDF格式
  10. android 编程w3c,w3cschool-编程学院