| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280 |
-
- using System;
- using System.Data;
- using System.IO;
- using System.Text;
- using System.Web;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- using NPOI.XSSF.UserModel;
- using CallCenterApi.DB;
- using System.Collections.Generic;
- namespace CallCenter.Utility
- {
- public class NPOIHelper
- {
- private string _title;
- private string _sheetName;
- private string _filePath;
- /// <summary>
- /// 导出到Excel
- /// </summary>
- /// <param name="table"></param>
- /// <returns></returns>
- public bool ToExcel(DataTable table, string[] columns = null)
- {
- FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- IWorkbook workBook = new HSSFWorkbook();
- if (string.IsNullOrWhiteSpace(this._sheetName))
- {
- this._sheetName = "sheet1";
- }
- ISheet sheet = workBook.CreateSheet(this._sheetName);
- //处理表格标题
- IRow row = sheet.CreateRow(0);
- row.CreateCell(0).SetCellValue(this._title);
- sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
- row.Height = 500;
- ICellStyle cellStyle = workBook.CreateCellStyle();
- IFont font = workBook.CreateFont();
- font.FontName = "微软雅黑";
- font.FontHeightInPoints = 17;
- cellStyle.SetFont(font);
- cellStyle.VerticalAlignment = VerticalAlignment.Center;
- cellStyle.Alignment = HorizontalAlignment.Center;
- row.Cells[0].CellStyle = cellStyle;
- //处理表格列头
- row = sheet.CreateRow(1);
- if (columns == null)
- {
- for (int i = 0; i < table.Columns.Count; i++)
- {
- row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
- row.Height = 350;
- sheet.AutoSizeColumn(i);
- }
- }
- else
- {
- for (int i = 0; i < columns.Length; i++)
- {
- row.CreateCell(i).SetCellValue(columns[i]);
- row.Height = 350;
- sheet.AutoSizeColumn(i);
- }
- }
- //处理数据内容
- for (int i = 0; i < table.Rows.Count; i++)
- {
- row = sheet.CreateRow(2 + i);
- row.Height = 250;
- for (int j = 0; j < table.Columns.Count; j++)
- {
- row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
- sheet.SetColumnWidth(j, 256 * 15);
- }
- }
- //写入数据流
- workBook.Write(fs);
- fs.Flush();
- fs.Close();
- return true;
- }
- /// <summary>
- /// 导出到Excel
- /// </summary>
- /// <param name="table"></param>
- /// <param name="title"></param>
- /// <param name="sheetName">空字符串或null的话默认sheet1</param>
- /// <param name="columns">自定义表格列头,默认null</param>
- /// <returns></returns>
- public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
- {
- this._title = title;
- this._sheetName = sheetName;
- this._filePath = filePath;
- return ToExcel(table, columns);
- }
- /// <summary>
- /// 弹出下载框导出excel
- /// </summary>
- /// <param name="Name"></param>
- /// <param name="dt"></param>
- /// <returns></returns>
- public string ExportToExcel(string Name, DataTable dt, string[] cols = null)
- {
- try
- {
- //if (dt.Rows.Count > 0)
- //{
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle HeadercellStyle = workbook.CreateCellStyle();
- HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.Alignment = HorizontalAlignment.Center;
- HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- HeadercellStyle.FillPattern = FillPattern.SolidForeground;
- HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- //字体
- NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
- headerfont.Boldweight = (short)FontBoldWeight.Bold;
- headerfont.FontHeightInPoints = 12;
- HeadercellStyle.SetFont(headerfont);
- //用column name 作为列名
- int icolIndex = 0;
- IRow headerRow = sheet.CreateRow(0);
- if (cols == null || (cols != null && cols.Length == 0))
- {
- foreach (DataColumn dc in dt.Columns)
- {
- ICell cell = headerRow.CreateCell(icolIndex);
- cell.SetCellValue(dc.ColumnName);
- cell.CellStyle = HeadercellStyle;
- icolIndex++;
- }
- }
- else
- {
- foreach (string dc in cols)
- {
- ICell cell = headerRow.CreateCell(icolIndex);
- cell.SetCellValue(dc);
- cell.CellStyle = HeadercellStyle;
- icolIndex++;
- }
- }
- ICellStyle cellStyle = workbook.CreateCellStyle();
- //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
- cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
- cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
- //建立内容行
- int iRowIndex = 0;
- foreach (DataRow dr in dt.Rows)
- {
- int iCellIndex = 0;
- IRow irow = sheet.CreateRow(iRowIndex + 1);
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- string strsj = string.Empty;
- if (dr[i] != null)
- {
- strsj = dr[i].ToString();
- }
- ICell cell = irow.CreateCell(iCellIndex);
- cell.SetCellValue(strsj);
- cell.CellStyle = cellStyle;
- iCellIndex++;
- }
- iRowIndex++;
- }
- //自适应列宽度
- for (int i = 0; i < icolIndex; i++)
- {
- sheet.AutoSizeColumn(i);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
- //}
- return "";
- }
- catch
- {
- return "导出失败!";
- }
- }
- /// <summary>
- /// 合并单元格
- /// </summary>
- /// <param name="sheet">要合并单元格所在的sheet</param>
- /// <param name="rowstart">开始行的索引</param>
- /// <param name="rowend">结束行的索引</param>
- /// <param name="colstart">开始列的索引</param>
- /// <param name="colend">结束列的索引</param>
- public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
- {
- CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
- sheet.AddMergedRegion(cellRangeAddress);
- }
- /// <summary>
- /// 工单类型弹出下载框导出excel
- /// </summary>
- /// <param name="Name"></param>
- /// <param name="dt"></param>
- /// <param name="typeclass">仪器或试剂</param>
- /// <returns></returns>
- public string ExportToExcelForGDLX(string Name, DataTable dt, string typeclass, List<string> colnames, List<int> erows, List<string> secolnames)
- {
- try
- {
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle HeadercellStyle = workbook.CreateCellStyle();
- HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.Alignment = HorizontalAlignment.Center;
- HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- HeadercellStyle.FillPattern = FillPattern.SolidForeground;
- HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- //字体
- NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
- headerfont.Boldweight = (short)FontBoldWeight.Bold;
- headerfont.FontHeightInPoints = 12;
- HeadercellStyle.SetFont(headerfont);
- //用column name 作为列名
- int icolIndex = 0;
- IRow headerRow = sheet.CreateRow(0);
-
- for (int i = 0; i < colnames.Count; i++)
- {
- ICell cell = headerRow.CreateCell(i);
- cell.SetCellValue(colnames[i]);
- cell.CellStyle = HeadercellStyle;
- //SetCellRangeAddress(sheet, 0, 0, erows[i * 2], erows[i * 2 + 1]);
- }
-
- for (int k = 0; k < erows.Count / 2; k++)
- {
- SetCellRangeAddress(sheet, 0, 0, erows[k * 2], erows[k * 2 + 1]);
- }
- //添加第二行标题
- IRow SecRow = sheet.CreateRow(1);
- for (int i = 0; i < secolnames.Count; i++)
- {
- ICell cell = SecRow.CreateCell(i);
- cell.SetCellValue(secolnames[i].ToString());
- }
- ICellStyle cellStyle = workbook.CreateCellStyle();
- //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
- cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
- cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
- //建立内容行
- int iRowIndex = 0;
- foreach (DataRow dr in dt.Rows)
- {
- int iCellIndex = 0;
- IRow irow = sheet.CreateRow(iRowIndex + 2);
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- string strsj = string.Empty;
- if (dr[i] != null)
- {
- strsj = dr[i].ToString();
- }
- ICell cell = irow.CreateCell(iCellIndex);
- cell.SetCellValue(strsj);
- cell.CellStyle = cellStyle;
- iCellIndex++;
- }
- iRowIndex++;
- }
- //自适应列宽度
- for (int i = 0; i < icolIndex; i++)
- {
- sheet.AutoSizeColumn(i);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
-
- return "";
- }
- catch (Exception e)
- {
- return "导出失败!" + e.Message;
- }
- }
- /// <summary>
- /// 导入excel转换为datatable
- /// </summary>
- /// <param name="upfile"></param>
- /// <param name="headrow"></param>
- /// <returns></returns>
- public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
- {
- DataTable dt = new DataTable();
- IWorkbook workbook = null;
- Stream stream = upfile.InputStream;
- string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
- if (suffix == "xlsx") // 2007版本
- {
- workbook = new XSSFWorkbook(stream);
- }
- else if (suffix == "xls") // 2003版本
- {
- workbook = new HSSFWorkbook(stream);
- }
- //获取excel的第一个sheet
- ISheet sheet = workbook.GetSheetAt(0);
- //获取sheet的第一行
- IRow headerRow = sheet.GetRow(headrow);
- //一行最后一个方格的编号 即总的列数
- int cellCount = headerRow.LastCellNum;
- //最后一列的标号 即总的行数
- int rowCount = sheet.LastRowNum;
- //列名
- for (int i = 0; i < cellCount; i++)
- {
- dt.Columns.Add(headerRow.GetCell(i).ToString());
- }
- for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
- {
- DataRow dr = dt.NewRow();
- IRow row = sheet.GetRow(i);
- for (int j = row.FirstCellNum; j < cellCount; j++)
- {
- if (row.GetCell(j) != null)
- {
- dr[j] = row.GetCell(j).ToString();
- }
- }
- dt.Rows.Add(dr);
- }
- sheet = null;
- workbook = null;
- return dt;
- }
- /// <summary>
- /// 导入excel转换为datatable
- /// </summary>
- /// <param name="upfile"></param>
- /// <param name="headrow"></param>
- /// <returns></returns>
- public DataTable ExcelToTable(string fileName, int headrow)
- {
- DataTable dt = new DataTable();
- IWorkbook workbook = null;
- Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
- string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
- if (suffix == "xlsx") // 2007版本
- {
- workbook = new XSSFWorkbook(stream);
- }
- else if (suffix == "xls") // 2003版本
- {
- workbook = new HSSFWorkbook(stream);
- }
- //获取excel的第一个sheet
- ISheet sheet = workbook.GetSheetAt(0);
- //获取sheet的第一行
- IRow headerRow = sheet.GetRow(headrow);
- //一行最后一个方格的编号 即总的列数
- int cellCount = headerRow.LastCellNum;
- //最后一列的标号 即总的行数
- int rowCount = sheet.LastRowNum;
- //列名
- for (int i = 0; i < cellCount; i++)
- {
- dt.Columns.Add(headerRow.GetCell(i).ToString());
- }
- for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
- {
- DataRow dr = dt.NewRow();
- IRow row = sheet.GetRow(i);
- for (int j = row.FirstCellNum; j < cellCount; j++)
- {
- if (row.GetCell(j) != null)
- {
- dr[j] = row.GetCell(j).ToString();
- }
- }
- dt.Rows.Add(dr);
- }
- sheet = null;
- workbook = null;
- return dt;
- }
- /// <summary>
- /// 简报导出
- /// </summary>
- /// <param name="ds"></param>
- /// <returns></returns>
- public string MarketExportToExcel(string stime,string etime,Market market )
- {
- try
- {
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle cellStyle = workbook.CreateCellStyle();
- //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
- //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
- //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
- ICellStyle cellStylebt = workbook.CreateCellStyle();
- NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
- cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
- cellStylebt.SetFont(cellfontbt);
- cellStylebt.VerticalAlignment = VerticalAlignment.Center;
- cellStylebt.Alignment = HorizontalAlignment.Center;
- IRow irow1 = sheet.CreateRow(1);
- ICell cell1 = irow1.CreateCell(0);
- cell1.SetCellValue("时间:" + stime + " 至 " + etime);
- cell1.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
- //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
- #region 产品投诉情况
- List<Ification> dt1 = market.product ;
- IRow irow2 = sheet.CreateRow(2);
- ICell cell2 = irow2.CreateCell(0);
- cell2.SetCellValue("产品投诉情况");
- cell2.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
- int dtrow = 3;
- if (dt1.Count >0)
- {
- int dt = 0;
- int a = dt1.Count / 3;
- float b = dt1.Count % 3;
- if (b >0)
- a = a + 1;
- dtrow += a;
- List<IRow> irow3list = new List<IRow>();
- for (int i =0;i <a;i++)
- {
- IRow irow3 = sheet.CreateRow(3 + i);
- irow3list.Add(irow3);
- }
- int td = 0;
- foreach (var it in dt1)
- {
- dt++;
- if (dt <4)
- {
- ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
- cell101.SetCellValue(it.name );
- cell101.CellStyle = cellStyle;
- ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
- cell102.SetCellValue(it .number );
- cell102.CellStyle = cellStyle;
- ICell cell103= irow3list[td].CreateCell(3 * dt-1 );
- cell103.SetCellValue(it.Proportion );
- cell103.CellStyle = cellStyle;
- }
- else
- {
- dt = 1;
- td++;
- ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
- cell101.SetCellValue(it.name);
- cell101.CellStyle = cellStyle;
- ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
- cell102.SetCellValue(it.number);
- cell102.CellStyle = cellStyle;
- ICell cell103 = irow3list[td].CreateCell(3 * dt-1);
- cell103.SetCellValue(it.Proportion);
- cell103.CellStyle = cellStyle;
- }
- }
-
- }
- #endregion
- #region 服务投诉情况
- Ification dt2 = market.service;
- dtrow++;
- IRow irow4 = sheet.CreateRow(dtrow);
- ICell cell4 = irow4.CreateCell(0);
- cell4.SetCellValue("服务投诉情况");
- cell4.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt2!=null )
- {
- dtrow++;
- IRow irow5 = sheet.CreateRow(dtrow);
- ICell cell51 = irow5.CreateCell(0);
- cell51.SetCellValue(dt2.name);
- cell51.CellStyle = cellStyle;
- ICell cell52 = irow5.CreateCell(1);
- cell52.SetCellValue(dt2.number);
- cell52.CellStyle = cellStyle;
- ICell cell53 = irow5.CreateCell(2);
- cell53.SetCellValue(dt2.Proportion);
- cell53.CellStyle = cellStyle;
- }
-
- #endregion
- #region 涉媒投诉
- Ification dt3 = market.sediainvolved;
- dtrow++;
- IRow irow9 = sheet.CreateRow(dtrow);
- ICell cell9 = irow9.CreateCell(0);
- cell9.SetCellValue("涉媒投诉");
- cell9.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt3!=null )
- {
- dtrow++;
- IRow irow10 = sheet.CreateRow(dtrow);
- ICell cell53 = irow10.CreateCell(0);
- cell53.SetCellValue(dt3.name);
- cell53.CellStyle = cellStyle;
- ICell cell54 = irow10.CreateCell(1);
- cell54.SetCellValue(dt3.number);
- cell54.CellStyle = cellStyle;
- ICell cell55 = irow10.CreateCell(2);
- cell55.SetCellValue(dt3.Proportion);
- cell55.CellStyle = cellStyle;
- }
-
- #endregion
- #region 市场抽检
- Ification dt4 = market.spotcheck;
- dtrow++;
- IRow irow11 = sheet.CreateRow(dtrow);
- ICell cell12 = irow11.CreateCell(0);
- cell12.SetCellValue("市场抽检");
- cell12.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt4!=null )
- {
- dtrow++;
- IRow irow12 = sheet.CreateRow(dtrow);
- ICell cell55 = irow12.CreateCell(0);
- cell55.SetCellValue(dt4.name);
- cell55.CellStyle = cellStyle;
- ICell cell56 = irow12.CreateCell(1);
- cell56.SetCellValue(dt4.number);
- cell56.CellStyle = cellStyle;
- ICell cell57 = irow12.CreateCell(2);
- cell57.SetCellValue(dt4.Proportion);
- cell57.CellStyle = cellStyle;
- }
-
- #endregion
- #region 其他信息
- Ification dt5 = market.other;
- dtrow++;
- IRow irow13 = sheet.CreateRow(dtrow);
- ICell cell13 = irow13.CreateCell(0);
- cell13.SetCellValue("其他信息");
- cell13.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt5!=null )
- {
- dtrow++;
- IRow irow15 = sheet.CreateRow(dtrow);
- ICell cell57 = irow15.CreateCell(0);
- cell57.SetCellValue(dt4.name);
- cell57.CellStyle = cellStyle;
- ICell cell58 = irow15.CreateCell(1);
- cell58.SetCellValue(dt4.number);
- cell58.CellStyle = cellStyle;
- ICell cell59 = irow15.CreateCell(2);
- cell59.SetCellValue(dt5.Proportion);
- cell59.CellStyle = cellStyle;
- }
-
- #endregion
- //自适应列宽度
- for (int i = 0; i < 8; i++)
- {
- sheet.AutoSizeColumn(i);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode("市场信息简报" + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
- return "";
- }
- catch
- {
- return "导出失败!";
- }
- }
- private void Columnwidth(int number, ISheet ffSheet)
- {
- for (int columnNum = 0; columnNum <= number; columnNum++)
- {
- int columnWidth = ffSheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度
- for (int rowNum = 1; rowNum <= ffSheet.LastRowNum; rowNum++)//在这一列上循环行
- {
- IRow currentRow = ffSheet.GetRow(rowNum);
- ICell currentCell = currentRow.GetCell(columnNum);
- int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取当前单元格的内容宽度
- if (columnWidth < length + 1)
- {
- columnWidth = length + 1;
- }//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度,后面的+1是我人为的将宽度增加一个字符
- }
- }
- }
- /// <summary>
- /// 投诉产品日期分布表
- /// </summary>
- /// <param name="ds"></param>
- /// <returns></returns>
- public string DistributionToExcel(DateTime datetime, Product product)
- {
- try
- {
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle cellStyle = workbook.CreateCellStyle();
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
- ICellStyle cellStylebt = workbook.CreateCellStyle();
- NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
- cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
-
- cellStylebt.SetFont(cellfontbt);
- cellStylebt.VerticalAlignment = VerticalAlignment.Center;
- cellStylebt.Alignment = HorizontalAlignment.Center;
- IRow irow1 = sheet.CreateRow(0);
- ICell cell1 = irow1.CreateCell(0);
- cell1.SetCellValue(datetime.Year +"年"+datetime .Month +"月份投诉产品日期分布情况表");
- cell1.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 13));
- #region 产品投诉情况
-
- IRow irow2 = sheet.CreateRow(1);
- ICell cell2 = irow2.CreateCell(0);
- ICellStyle style13 = workbook.CreateCellStyle();
- style13.BorderDiagonalLineStyle = BorderStyle.Thin;
- style13.BorderDiagonal = BorderDiagonal.Backward;
- style13.BorderDiagonalColor = IndexedColors.Black .Index;
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("数量".PadLeft(15));//该行不足长度10在左侧填空格
- sb.AppendLine("日期".PadRight(15));//该行不足长度10在右侧填空格
- cell2.SetCellValue(sb.ToString ());
- cell2.CellStyle = cellStylebt;
- irow2.GetCell(0).CellStyle = style13;
- sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0,0 ));
- ICell cell6 = irow2.CreateCell(1);
- cell6.SetCellValue("投诉产品");
- cell6.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 1));
- for (int i=0;i<5;i ++)
- {
- string msg = "";
- switch (i)
- {
- case 0:
- msg = datetime.AddMonths(-11).Month + "-" + datetime.AddMonths(-4).Month + "月份日期";
- break;
- case 1:
- msg = datetime.AddMonths(-3).Month + "月份日期";
- break;
- case 2:
- msg = datetime.AddMonths(-2).Month + "月份日期";
- break;
- case 3:
- msg = datetime.AddMonths(-1).Month + "月份日期";
- break;
- case 4:
- msg = "不清楚日期";
- break;
- }
- ICell cell3 = irow2.CreateCell(i *2+ 2 );
- cell3.SetCellValue(msg);
- cell3.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, i * 2 + 2, i * 2 + 3));
- }
- IRow irow3 = sheet.CreateRow(2);
- for (int i = 0; i < 11; i++)
- {
- string msg = "";
- if (i ==0)
- {
- msg = "数量";
- }
- else
- {
- if (i % 2 == 0)
- {
- msg = "占比";
- }
- else
- {
- msg = "数量";
- }
- }
- ICell cell4 = irow3.CreateCell(i+1);
- cell4.SetCellValue(msg);
- cell4.CellStyle = cellStylebt;
-
- }
- List<Date> dt1 = product.dates;
- if (dt1!=null )
- {
- for (int i=0;i < dt1.Count;i ++)
- {
- IRow irow4 = sheet.CreateRow(3 + i);
- string msg = "";
- for (int j = 0; j < 12; j ++)
- {
- switch (j )
- {
- case 0:
- msg = dt1[i].name;
- break;
- case 1:
- msg = dt1[i].total.ToString();
- break;
- case 2:
- msg = dt1[i].MonthCount1 .ToString ();
- break;
- case 3:
- msg = dt1[i].MonthCountmix1 .ToString();
- break;
- case 4:
- msg = dt1[i].MonthCount2.ToString();
- break;
- case 5:
- msg = dt1[i].MonthCountmix2.ToString();
- break;
- case 6:
- msg = dt1[i].MonthCount3.ToString();
- break;
- case 7:
- msg = dt1[i].MonthCountmix3.ToString();
- break;
- case 8:
- msg = dt1[i].MonthCount4.ToString();
- break;
- case 9:
- msg = dt1[i].MonthCountmix4.ToString();
- break;
- case 10:
- msg = dt1[i].MonthCount5.ToString();
- break;
- case 11:
- msg = dt1[i].MonthCountmix5.ToString();
- break;
-
- }
- ICell cell5 = irow4.CreateCell(j);
- cell5.SetCellValue(msg);
- cell5.CellStyle = cellStylebt;
- }
- }
- }
- int t = dt1.Count + 3;int count = 0;
- List<Factory> Factory1 = product.factory;
- List<Factory> Factory2 = product.problem;
- List<Factory> Factory3 = product.product;
- int factory = 0, problem = 0, productcode = 0;
- if (Factory1 != null)
- factory = (Factory1.Count / 2) + (0 == Factory1.Count % 2 ? 0 : 1);
- if (Factory2 != null)
- problem = Factory2.Count;
- if (Factory3 != null)
- productcode = Factory3.Count;
- bool istrue = true; int a = 0, total = problem + productcode;
- if (factory>(total))
- {
- count = factory;
- a = factory - total;
- }
- else
- {
- istrue = false;
- count = total;
- }
-
- IRow irow5 = sheet.CreateRow(t);
- ICell cell7 = irow5.CreateCell(0);
- cell7.SetCellValue("工\n厂\n投\n诉\n占\n比\n");
- cell7.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t, t+ count, 0, 0));
- ICell cell10 = irow5.CreateCell(7);
- cell10.SetCellValue("各\n质\n量\n问\n题\n占\n比\n");
- cell10.CellStyle = cellStylebt;
- int structure = 0;
- if (istrue)
- {
- if (a >1)
- {
- structure = t + problem + a / 2;
- sheet.AddMergedRegion(new CellRangeAddress(t, t + problem + a / 2, 7, 7));
- }
-
- else
- {
- structure = t + problem;
- sheet.AddMergedRegion(new CellRangeAddress(t, t + problem + 1, 7, 7));
- }
- }
- else
- sheet.AddMergedRegion(new CellRangeAddress(t, t + problem, 7, 7));
- IRow irow7 = sheet.CreateRow(structure);
- ICell cell11 = irow7.CreateCell(7);
- cell11.SetCellValue("结\n构\n占\n比\n");
- cell11.CellStyle = cellStylebt;
- if (istrue)
- {
- if (a > 1)
- sheet.AddMergedRegion(new CellRangeAddress(t + problem + a / 2+1, t + total + 1 +a/2, 7, 7));
- else
- sheet.AddMergedRegion(new CellRangeAddress(t + problem + 2, t + total + 2, 7, 7));
- }
- else
- sheet.AddMergedRegion(new CellRangeAddress(t + problem+1, t + 1 + total, 7, 7));
- if (Factory1 != null)
- {
- for (int i = 0; i < count; i++)
- {
- if (i ==0 || t + i== structure)
- {
- if (i < Factory1.Count )
- {
- for (int j = 0; j < 6; j++)
- {
- string msg = "";
- switch (j)
- {
- case 0:
- msg = Factory1[i * 2].name;
- break;
- case 1:
- msg = Factory1[i * 2].number.ToString();
- break;
- case 2:
- msg = Factory1[i * 2].proportion;
- break;
- case 3:
- msg = Factory1[i * 2 + 1].name;
- break;
- case 4:
- msg = Factory1[i * 2 + 1].number.ToString();
- break;
- case 5:
- msg = Factory1[i * 2 + 1].proportion;
- break;
- }
- if (i == 0)
- {
- ICell cell8 = irow5.CreateCell(j + 1);
- cell8.SetCellValue(msg);
- cell8.CellStyle = cellStylebt;
- }
- else
- {
- ICell cell8 = irow7.CreateCell(j + 1);
- cell8.SetCellValue(msg);
- cell8.CellStyle = cellStylebt;
- }
- }
- }
- if (i ==0)
- {
- for (int z = 0; z < 3; z++)
- {
- string msg = "";
- switch (z)
- {
- case 0:
- ICell cell8 = irow5.CreateCell(8);
- cell8.SetCellValue(Factory2[0].name );
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t, t , 8, 9));
- break;
- case 1:
- ICell cell9 = irow5.CreateCell(10);
- cell9.SetCellValue(Factory2[0].number .ToString ());
- cell9.CellStyle = cellStylebt;
-
- break;
- case 2:
- ICell cell12 = irow5.CreateCell(11);
- cell12.SetCellValue(Factory2[0].proportion);
- cell12.CellStyle = cellStylebt;
- break;
- }
-
- }
- }
- else if (t + i == structure)
- {
- for (int z = 0; z < 3; z++)
- {
- string msg = "";
- switch (z)
- {
- case 0:
- ICell cell8 = irow7.CreateCell(8);
- cell8.SetCellValue(Factory3[1].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(structure, structure, 8, 9));
- break;
- case 1:
- ICell cell9 = irow7.CreateCell(10);
- cell9.SetCellValue(Factory3[1].number.ToString());
- cell9.CellStyle = cellStylebt;
- break;
- case 2:
- ICell cell12 = irow7.CreateCell(11);
- cell12.SetCellValue(Factory3[1].proportion);
- cell12.CellStyle = cellStylebt;
- break;
- }
- }
- }
-
- }
- else
- {
-
- IRow irow6 = sheet.CreateRow(t + i);
- if (i < Factory1.Count)
- {
- for (int j = 0; j < 6; j++)
- {
- string msg = "";
- switch (j)
- {
- case 0:
- msg = Factory1[i * 2].name;
- break;
- case 1:
- msg = Factory1[i * 2].number.ToString();
- break;
- case 2:
- msg = Factory1[i * 2].proportion;
- break;
- case 3:
- msg = Factory1[i * 2 + 1].name;
- break;
- case 4:
- msg = Factory1[i * 2 + 1].number.ToString();
- break;
- case 5:
- msg = Factory1[i * 2 + 1].proportion;
- break;
- }
- ICell cell9 = irow6.CreateCell(j + 1);
- cell9.SetCellValue(msg);
- cell9.CellStyle = cellStylebt;
- }
- }
- if (istrue)
- {
- int b = 1;
- if (a >1)
- {
- b= (a / 2) + (0 == a % 2 ? 0 : 1);
- }
- if (i < Factory2.Count -1)
- {
- for (int z = 0; z < 3; z++)
- {
- string msg = "";
- switch (z)
- {
- case 0:
- ICell cell8 = irow6.CreateCell(8);
- cell8.SetCellValue(Factory2[i].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i, 8, 9));
- break;
- case 1:
- ICell cell9 = irow6.CreateCell(10);
- cell9.SetCellValue(Factory2[i].number.ToString());
- cell9.CellStyle = cellStylebt;
- break;
- case 2:
- ICell cell12 = irow6.CreateCell(11);
- cell12.SetCellValue(Factory2[i].proportion);
- cell12.CellStyle = cellStylebt;
- break;
- }
- }
- }
- else if (i < Factory2.Count )
- {
- for (int z = 0; z < 3; z++)
- {
- string msg = "";
- switch (z)
- {
- case 0:
- ICell cell8 = irow7.CreateCell(8);
- cell8.SetCellValue(Factory2[i].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i+b , 8, 9));
- break;
- case 1:
- ICell cell9 = irow7.CreateCell(10);
- cell9.SetCellValue(Factory2[i].number.ToString());
- cell9.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 10, 10));
- break;
- case 2:
- ICell cell12 = irow7.CreateCell(11);
- cell12.SetCellValue(Factory2[i].proportion);
- cell12.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 11, 11));
- break;
- }
- }
- }
- if (i <( Factory3.Count + Factory2.Count+b -1)&& i>= Factory2.Count + b)
- {
- for (int z = 0; z < 3; z++)
- {
- string msg = "";
- switch (z)
- {
- case 0:
- ICell cell8 = irow6.CreateCell(8);
- cell8.SetCellValue(Factory3[i- Factory2.Count -b ].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i, 8, 9));
- break;
- case 1:
- ICell cell9 = irow6.CreateCell(10);
- cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
- cell9.CellStyle = cellStylebt;
- break;
- case 2:
- ICell cell12 = irow6.CreateCell(11);
- cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
- cell12.CellStyle = cellStylebt;
- break;
- }
- }
- }
- else if ( i >= Factory2.Count + b&& i < Factory2.Count + b+ Factory3.Count )
- {
- for (int z = 0; z < 3; z++)
- {
- string msg = "";
- switch (z)
- {
- case 0:
- ICell cell8 = irow7.CreateCell(8);
- cell8.SetCellValue(Factory3[i - Factory2.Count - b].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 8, 9));
- break;
- case 1:
- ICell cell9 = irow7.CreateCell(10);
- cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
- cell9.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 10, 10));
- break;
- case 2:
- ICell cell12 = irow7.CreateCell(11);
- cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
- cell12.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + i, t + i + b, 11, 11));
- break;
- }
- }
- }
- }
-
- }
- }
- }
-
- #endregion
- //自适应列宽度
- for (int i = 0; i < 12; i++)
- {
- // sheet.AutoSizeColumn(i);
- sheet.SetColumnWidth(i, 15 * 256);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode("投诉产品日期分布表" + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
- return "";
- }
- catch
- {
- return "导出失败!";
-
- }
- }
- }
- }
|