public string print(string printListJson) { string error = "NULL"; if (CheckTools.isNull(printListJson)) { return(error); } PrinterList printerList = JsonConvert.DeserializeObject <PrinterList>(printListJson); if (printerList == null) { return(error); } if (printerList.command == null) { printerList.command = "print"; } if (string.Equals("print", printerList.command, StringComparison.CurrentCultureIgnoreCase)) { print(true, printerList); error = printerList.errorMsg; } else { error = printListJson; } //StringBuilder sb = new StringBuilder(printerList.listId == null ? "" : printerList.listId.ToString()).Append("ACK".Equals(error) ? "打印成功!" : error); //error = sb.ToString(); //sb.Clear(); return(error); }
private ExcelTemplateBean CellValueRegex(string cellValue) { if (CheckTools.isNull(cellValue)) { return(new ExcelTemplateBean(-1)); } cellValue = cellValue.Trim(); string fieldId = null; string fieldName = null; Match match = fieldRegex.Match(cellValue); if (match.Success) { fieldName = match.Groups[1].Value; //Console.WriteLine(fieldName); return(new ExcelTemplateBean(1, fieldName)); } match = ListRegex.Match(cellValue); if (match.Success) { fieldId = match.Groups[1].Value; fieldName = match.Groups[2].Value; return(new ExcelTemplateBean(2, fieldId, fieldName)); } return(new ExcelTemplateBean(0)); }
private void readIni() { Ini ini = new Ini(Application.StartupPath + Path.DirectorySeparatorChar + "user.ini"); string address = ini.Read("user", "address", 256); if (CheckTools.isUrl(address)) { this.txt_print_address.Text = address; } string company = ini.Read("user", "company", 256); if (!CheckTools.isNull(company)) { this.txt_company_code_port.Text = FingerUtil.Decrypt(company, key); } else { txt_company_code_port.Text = ""; } string name = ini.Read("user", "name", 256); if (!CheckTools.isNull(name)) { this.txt_login_name.Text = FingerUtil.Decrypt(name, key); } string password = ini.Read("user", "password", 256); if (!CheckTools.isNull(password)) { this.txt_login_password.Text = FingerUtil.Decrypt(password, key); } }
public PrinterList print(bool isPrint, PrinterList printerList) { string error = null; if (printerList == null) { error = "无可用打印数据"; printerList.printResult = 2; printerList.errorMsg = error; return(printerList); } if (CheckTools.isNull(printerList.templateFileName)) { error = "打印模板名称不允许为空"; printerList.printResult = 2; printerList.errorMsg = error; return(printerList); } IDictionary <string, string> fixedField = null; if (!CheckTools.isNull(printerList.printerJson)) { try { fixedField = JsonConvert.DeserializeObject <IDictionary <string, string> >(printerList.printerJson); } catch (Exception e) { //Console.WriteLine("CallServiceThread序列化异常:" + e.Message); error = e.Message; } } IDictionary <string, DataTable> listField = null; if (!CheckTools.isNull(printerList.printerListJson)) { try { listField = JsonConvert.DeserializeObject <IDictionary <string, DataTable> >(printerList.printerListJson); } catch (Exception e) { //Console.WriteLine("CallServiceThread序列化异常:" + e.Message); error = e.Message; } } //序列化出错 if (error != null) { printerList.printResult = 2; printerList.errorMsg = error; return(printerList); } if ((fixedField == null || fixedField.Count == 0) && (listField == null || listField.Count == 0)) { error = "打印模板内容参数printerJson或printerListJson不允许同时为空"; printerList.printResult = 2; printerList.errorMsg = error; return(printerList); } string filename; int dot = printerList.templateFileName.IndexOf("."); if (dot == -1) { filename = printerList.templateFileName; printerList.templateFileName += ".xlsx"; } else { filename = printerList.templateFileName.Substring(0, dot); } string exportOutFileName = filename + "_"; if (printerList.listId == null) { exportOutFileName += StaticUtil.ConvertDateTimeToTimestamp(printerList.requestTime); } else { exportOutFileName += printerList.listId; } try { string excelFileName = new ExcelUtil().TemplateExport(printerList.templateFileName, exportOutFileName, fixedField, listField); if (printerList.command == null) { printerList.command = "print"; } if (string.Equals("print", printerList.command, StringComparison.CurrentCultureIgnoreCase)) { StaticUtil.Print(excelFileName); } //打印结果0待打印1打印成功2打印失败 printerList.printResult = 1; printerList.errorMsg = "ACK"; } catch (Exception e) { Console.WriteLine("CallServiceThread.ExcelUtil.TemplateExport异常" + e); error = e.Message; printerList.printResult = 2; printerList.errorMsg = error; } return(printerList); }
public string TemplateExport(string templateFileNameAndExtension, string exportOutFileName, IDictionary <string, string> fixedField, IDictionary <string, DataTable> listField) { IWorkbook workbook = null; FileStream inFileStream = null; FileStream outFileStream = null; try { if ((fixedField != null && fixedField.Count > 0)) { fieldList = new List <ExcelTemplateBean>(fixedField.Count); } if ((listField != null && listField.Count > 0)) { dicList = new Dictionary <string, List <ExcelTemplateBean> >(listField.Count); } if (fieldList == null && dicList == null) { return(null); } string fileDir = Application.StartupPath; string fileName = fileDir + Path.DirectorySeparatorChar + "resource" + Path.DirectorySeparatorChar + "template" + Path.DirectorySeparatorChar + templateFileNameAndExtension; //Console.WriteLine("模板文件路径:"+fileName); inFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); string extension = Path.GetExtension(fileName); //if (".xlsx".Equals(extension)) //{ // workbook = new XSSFWorkbook(inFileStream); //} //else if (".xls".Equals(extension)) //{ // workbook = new HSSFWorkbook(inFileStream); //} //else //{ // return null; //} if (!".xls".Equals(extension) && !".xlsx".Equals(extension)) { return(null); } workbook = WorkbookFactory.Create(inFileStream); ScanField(workbook); ISheet sheet = workbook.GetSheetAt(0); IRow row = null; ICell cell = null; string cellValue = null; string regex = null; string fieldValue = null; //写固定字段 if (fieldList != null) { foreach (ExcelTemplateBean bean in fieldList) { row = sheet.GetRow(bean.RowIndex); cell = row.GetCell(bean.ColumnIndex); if (cell == null) { continue; } cellValue = cell.ToString(); regex = "f{" + bean.FieldName + "}"; if (CheckTools.isNull(cellValue)) { cellValue = regex; } if (fixedField.ContainsKey(bean.FieldName)) { fieldValue = fixedField[bean.FieldName]; } else { fieldValue = null; } if (fieldValue == null) { fieldValue = ""; } cell.SetCellValue(cellValue.Replace(regex, fieldValue)); } } //写列表字段 if (dicList != null) { int realInsertedRows = 0; string listId; DataTable dataTable = null; List <ExcelTemplateBean> beanList; foreach (KeyValuePair <string, List <ExcelTemplateBean> > pair in dicList) { listId = pair.Key; beanList = pair.Value; //List<ExcelTemplateBean>属于同一行数据 if (listField.ContainsKey(listId)) { dataTable = listField[listId]; } else { dataTable = new DataTable(); //创建空行 DataRow dr = dataTable.NewRow(); dataTable.Rows.Add(dr); foreach (ExcelTemplateBean bean in beanList) { //动态添加列 dataTable.Columns.Add(bean.FieldName, typeof(string)); //通过列名赋值 dr[bean.FieldName] = ""; } } int minRowIndex = MinRowIndex(beanList); if (minRowIndex == -1 || minRowIndex == int.MaxValue) { continue; } int templateStartRowIndex = minRowIndex + realInsertedRows; //计算变化后的起码索引号 int NumInsertedRows = dataTable.Rows.Count - 1; //少插入一行,模板自身占了一行 int realInsertingRows = InsertRows(sheet, templateStartRowIndex, NumInsertedRows); //本次插入的实际行数 int multipleInserted = NumInsertedRows == 0 ? 0 : (realInsertingRows / NumInsertedRows); for (int i = 0; i < dataTable.Rows.Count; i++) { DataRow dr = dataTable.Rows[i]; foreach (ExcelTemplateBean bean in beanList) { row = sheet.GetRow(bean.RowIndex + realInsertedRows + i * multipleInserted); cell = row.GetCell(bean.ColumnIndex);//列不变 if (cell == null) { continue; } cellValue = cell.ToString(); regex = "f{" + bean.FieldId + "}{" + bean.FieldName + "}"; if (CheckTools.isNull(cellValue)) { cellValue = regex; } if (dr.Table.Columns.Contains(bean.FieldName) && dr[bean.FieldName] != null) { fieldValue = dr[bean.FieldName].ToString(); } else { fieldValue = null; } if (fieldValue == null) { fieldValue = ""; } cell.SetCellValue(cellValue.Replace(regex, fieldValue)); } } realInsertedRows += realInsertingRows;//多数据模板行时累加插入的实际行数 } } string outFileName = fileDir + Path.DirectorySeparatorChar + "resource" + Path.DirectorySeparatorChar + "export" + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd") + Path.DirectorySeparatorChar + exportOutFileName + extension; string parentPath = Path.GetDirectoryName(outFileName); if (false == Directory.Exists(parentPath)) { //创建pic文件夹 Directory.CreateDirectory(parentPath); } outFileStream = new FileStream(outFileName, FileMode.Create, FileAccess.Write); workbook.Write(outFileStream); outFileStream.Flush(); return(outFileName); } finally { if (inFileStream != null) { inFileStream.Close(); } if (outFileStream != null) { outFileStream.Close(); } if (workbook != null) { workbook.Close(); } } }