///////////////////////////////////////////////////////////////// // // (C) Copyright 2013, Kenneth, Inc. // All rights reserved. Confidential. Except as pursuant // to a written agreement with Kenneth, this software may // not be used or distributed. This software may be covered // by one or more patents. // // 本软件为Kenneth开发,版权所有,违者必究,320325198102218110,23810511@qq.com // ///////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; using System.Reflection; using System.Data; using System.Data.SqlClient; using System.Transactions; using QuiltingCommon; using MySql.Data.MySqlClient; namespace QuiltingBusiness { public partial class Testdotpart { #region private const functions private static object lockobj = new object(); private const string _onlyTableName="TestDotPart"; private const string _fields = "t.RecId,t.PartCode,t.PartName,t.PartType,t.PartIndex,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive"; private const string _tableName = "TestDotPart t"; private const string _log_fields = "t.LogId,t.RecId,t.PartCode,t.PartName,t.PartType,t.PartIndex,t.CreateBy,t.CreateTime,t.ModifyBy,t.ModifyTime,t.IsActive,t.OperTime,t.OperUser,t.OperType,t.OperInfo"; private const string _log_tableName = "TestDotPart_log t"; private static string QueryFromWhere(string dateFrom, DateTime dtStart, DateTime dtEnd, string keyword1, string keyword2, string createby = null) { string fromwhere = string.Empty; fromwhere = fromwhere + ((createby == null || createby == "") ? "" : string.Format(" and t.CreateBy = '{0}' ", createby)); fromwhere = fromwhere + ((dateFrom == null || dateFrom == "") ? "" : string.Format(" and t.CreateTime >= '{0}' and t.CreateTime <= '{1}' ", dtStart, dtEnd)); fromwhere = fromwhere + ((keyword1 == null || keyword1 == "") ? "" : string.Format(" and (t.PartCode like '%{0}%' or t.PartName like '%{0}%') ", keyword1)); fromwhere = fromwhere + ((keyword2 == null || keyword2 == "") ? "" : string.Format(" and (t.PartCode like '%{0}%' or t.PartName like '%{0}%') ", keyword2)); fromwhere = fromwhere + " and t.IsActive > 0 "; return fromwhere; } private static void InsertToLog(MySqlConnection connection, string recid, string currentUserId, string opertype) { TestdotpartEntity data = LoadResult(connection, recid); if (data != null) { MySqlParameter paramLogId = new MySqlParameter("@LogId", MySqlDbType.VarChar) { Value = Guid.NewGuid().ToString() }; MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = data.RecId }; MySqlParameter paramPartCode = new MySqlParameter("@PartCode", MySqlDbType.VarChar) { Value = data.PartCode }; MySqlParameter paramPartName = new MySqlParameter("@PartName", MySqlDbType.VarChar) { Value = data.PartName }; MySqlParameter paramPartType = new MySqlParameter("@PartType", MySqlDbType.VarChar) { Value = data.PartType }; MySqlParameter paramPartIndex = new MySqlParameter("@PartIndex", MySqlDbType.VarChar) { Value = data.PartIndex }; MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = data.CreateBy }; MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = data.CreateTime }; MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = data.ModifyBy }; MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = data.ModifyTime }; MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.VarChar) { Value = data.IsActive }; MySqlParameter paramOperType = new MySqlParameter("@OperType", MySqlDbType.VarChar) { Value = opertype }; MySqlParameter paramOperUser = new MySqlParameter("@OperUser", MySqlDbType.VarChar) { Value = currentUserId }; MySqlParameter paramOperTime = new MySqlParameter("@OperTime", MySqlDbType.DateTime) { Value = DateTime.Now }; MySqlParameter paramOperInfo = new MySqlParameter("@OperInfo", MySqlDbType.VarChar) { Value = "" }; MysqlHelper.ExecuteNonQuery(connection, "insert into TestDotPart_log (LogId,RecId,PartCode,PartName,PartType,PartIndex,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive,OperType,OperUser,OperTime,OperInfo) values (@LogId,@RecId,@PartCode,@PartName,@PartType,@PartIndex,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive,@OperType,@OperUser,@OperTime,@OperInfo)", new MySqlParameter[] { paramLogId, paramRecId, paramPartCode, paramPartName, paramPartType, paramPartIndex, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive, paramOperType, paramOperUser, paramOperTime, paramOperInfo }); } } private static TestdotpartEntityExtension LoadResult(MySqlConnection connection, string recid) { MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid }; DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select * from TestDotPart t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId); TestdotpartEntityExtension data = MysqlHelper.DataRow0ToEntity(dt) as TestdotpartEntityExtension; return data; } private static TestdotpartEntityExtension LoadResult(string recid) { MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = recid }; DataTable dt = MysqlHelper.ExecuteDataTable("select * from TestDotPart t where t.RecId = @RecId and t.IsActive > 0 limit 1", paramRecId); TestdotpartEntityExtension data = MysqlHelper.DataRow0ToEntity(dt) as TestdotpartEntityExtension; if (data != null) { ConvertSingleData(data); } return data; } private static string GetConditionValue(List ce, string keyword) { if(ce.Where(q => q.Name.Equals(keyword)).FirstOrDefault() != null) { return ce.Where(q => q.Name.Equals(keyword, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault().Value; } return string.Empty; } #endregion public static string LoadResults(string currentUserId, string currentClientId, string currentSendParameter) { PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity; List ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List)) as List; string createby = GetConditionValue(ce, "CreateBy"); string keyword1 = GetConditionValue(ce, "KeyWord1"); string keyword2 = GetConditionValue(ce, "KeyWord2"); string dateFrom = GetConditionValue(ce, "DateFrom"); string dateTo = GetConditionValue(ce, "DateTo"); DateTime dtStart; DateTime dtEnd; if (!DateTime.TryParse(dateFrom, out dtStart)) dtStart = new DateTime(1900, 1, 1); if (!DateTime.TryParse(dateTo, out dtEnd)) dtEnd = new DateTime(2100, 1, 1); int count = 0; string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2, createby); DataTable dt = MysqlHelper.GetPager(out count, _fields, _tableName, fromwhere, se.OrderBy, se.SortOrder, se.PageIndex, se.Rows); List rows = MysqlHelper.DataTableToIList(dt) as List; foreach (var row in rows) { ConvertSingleData(row); } RecordEntity result = new RecordEntity(); result.total = (int)Math.Ceiling(count * 1.0 / se.Rows); result.page = se.PageIndex; result.records = count; result.rows = rows; OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(result) }; return JsonConvert.SerializeObject(oe); } public static string SaveResult(string currentUserId, string currentClientId, string currentSendParameter) { lock(lockobj) { var se = JsonConvert.DeserializeObject(currentSendParameter, typeof(BaseSendEntity)) as BaseSendEntity; using (var tranScope = new TransactionScope()) { string connectionString = AppHelper.GetLinkString(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { TestdotpartEntity send = JsonConvert.DeserializeObject(se.SendKey, typeof(TestdotpartEntity)) as TestdotpartEntity; MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId }; MySqlParameter paramPartCode = new MySqlParameter("@PartCode", MySqlDbType.VarChar) { Value = send.PartCode }; MySqlParameter paramPartName = new MySqlParameter("@PartName", MySqlDbType.VarChar) { Value = send.PartName }; MySqlParameter paramPartType = new MySqlParameter("@PartType", MySqlDbType.VarChar) { Value = send.PartType }; MySqlParameter paramPartIndex = new MySqlParameter("@PartIndex", MySqlDbType.VarChar) { Value = send.PartIndex }; MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = currentUserId }; MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = send.CreateTime == null ? System.DateTime.Now : send.CreateTime }; MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = currentUserId }; MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = send.ModifyTime == null ? System.DateTime.Now : send.ModifyTime }; MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.Int16) { Value = 1 }; DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select PartCode from TestDotPart where RecId <> @RecId and PartCode = @PartCode and IsActive > 0 limit 1", paramRecId, paramPartCode); if (dt.Rows.Count > 0) { OutEntity oex1 = new OutEntity() { ErrorCode = ErrorCode.UnexpectError, ErrorMessage = "当前编号:" + dt.Rows[0][0].ToString() + "已添加" }; return JsonConvert.SerializeObject(oex1); } dt = MysqlHelper.ExecuteDataTable(connection, "select RecId from TestDotPart where RecId = @RecId and IsActive > 0 limit 1", paramRecId); var opertype = ""; if (dt.Rows.Count == 0) { opertype = ((int)AppEnum.OperType.New).ToString(); if (string.IsNullOrEmpty(send.RecId)) { paramRecId.Value = Guid.NewGuid().ToString(); } paramIsActive.Value = 1; MysqlHelper.ExecuteNonQuery(connection, "insert into TestDotPart (RecId,PartCode,PartName,PartType,PartIndex,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@PartCode,@PartName,@PartType,@PartIndex,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)", new MySqlParameter[] { paramRecId, paramPartCode, paramPartName, paramPartType, paramPartIndex, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive }); } else { opertype = ((int)AppEnum.OperType.Modify).ToString(); MysqlHelper.ExecuteNonQuery(connection, "update TestDotPart set PartCode = @PartCode, PartName = @PartName, PartType = @PartType, PartIndex = @PartIndex, ModifyBy = @ModifyBy, ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId", new MySqlParameter[] { paramPartCode, paramPartName, paramPartType, paramPartIndex, paramModifyBy, paramModifyTime, paramIsActive, paramRecId }); } //更新扩展字段 SysDefineExtensionValue.SaveResultForTable(connection, currentUserId, currentClientId, se.SendPair, _onlyTableName, paramRecId.Value.ToString()); //save log InsertToLog(connection, paramRecId.Value.ToString(), currentUserId, opertype); //更新池 TestdotpartPool.Update(LoadResult(connection, paramRecId.Value.ToString())); tranScope.Complete(); OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = paramRecId.Value.ToString() }; return JsonConvert.SerializeObject(oe); } } } } public static string SaveResultByCode(MySqlConnection connection, string currentUserId, TestdotpartEntity send) { lock(lockobj) { MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = send.RecId }; MySqlParameter paramPartCode = new MySqlParameter("@PartCode", MySqlDbType.VarChar) { Value = send.PartCode }; MySqlParameter paramPartName = new MySqlParameter("@PartName", MySqlDbType.VarChar) { Value = send.PartName }; MySqlParameter paramPartType = new MySqlParameter("@PartType", MySqlDbType.VarChar) { Value = send.PartType }; MySqlParameter paramPartIndex = new MySqlParameter("@PartIndex", MySqlDbType.VarChar) { Value = send.PartIndex }; MySqlParameter paramCreateBy = new MySqlParameter("@CreateBy", MySqlDbType.VarChar) { Value = currentUserId }; MySqlParameter paramCreateTime = new MySqlParameter("@CreateTime", MySqlDbType.DateTime) { Value = send.CreateTime == null ? System.DateTime.Now : send.CreateTime }; MySqlParameter paramModifyBy = new MySqlParameter("@ModifyBy", MySqlDbType.VarChar) { Value = currentUserId }; MySqlParameter paramModifyTime = new MySqlParameter("@ModifyTime", MySqlDbType.DateTime) { Value = send.ModifyTime == null ? System.DateTime.Now : send.ModifyTime }; MySqlParameter paramIsActive = new MySqlParameter("@IsActive", MySqlDbType.Int16) { Value = 1 }; DataTable dt = MysqlHelper.ExecuteDataTable(connection, "select RecId from TestDotPart where PartCode = @PartCode and IsActive > 0 limit 1", paramPartCode); var opertype = ""; if (dt.Rows.Count == 0) { opertype = ((int)AppEnum.OperType.New).ToString(); paramRecId.Value = Guid.NewGuid().ToString(); paramIsActive.Value = 1; MysqlHelper.ExecuteNonQuery(connection, "insert into TestDotPart (RecId,PartCode,PartName,PartType,PartIndex,CreateBy,CreateTime,ModifyBy,ModifyTime,IsActive) values (@RecId,@PartCode,@PartName,@PartType,@PartIndex,@CreateBy,@CreateTime,@ModifyBy,@ModifyTime,@IsActive)", new MySqlParameter[] { paramRecId, paramPartCode, paramPartName, paramPartType, paramPartIndex, paramCreateBy, paramCreateTime, paramModifyBy, paramModifyTime, paramIsActive }); } else { opertype = ((int)AppEnum.OperType.Modify).ToString(); paramRecId.Value = dt.Rows[0][0].ToString(); MysqlHelper.ExecuteNonQuery(connection, "update TestDotPart set PartCode = @PartCode, PartName = @PartName, PartType = @PartType, PartIndex = @PartIndex, ModifyBy = @ModifyBy,ModifyTime = @ModifyTime,IsActive = @IsActive where RecId = @RecId", new MySqlParameter[] { paramPartCode, paramPartName, paramPartType, paramPartIndex, paramModifyBy, paramModifyTime, paramIsActive, paramRecId }); } //更新池,由于事物可能未提交,先不更新缓存,在提交后在更新缓存 //TestdotpartPool.Update(LoadResult(connection, paramRecId.Value.ToString())); //save log InsertToLog(connection, paramRecId.Value.ToString(), currentUserId, opertype); return paramRecId.Value.ToString(); } } public static string SaveResultByCode(string currentUserId, TestdotpartEntity send) { string recId = string.Empty; using (var tranScope = new TransactionScope()) { string connectionString = AppHelper.GetLinkString(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { recId = SaveResultByCode(connection, currentUserId, send); tranScope.Complete(); //更新池 TestdotpartPool.Update(LoadResult(connection, recId)); } } return recId; } public static string LoadResult(string currentUserId, string currentClientId, string currentSendParameter) { var send = JsonConvert.DeserializeObject(currentSendParameter, typeof(TestdotpartEntity)) as TestdotpartEntity; var data = LoadResult(send.RecId); var extend = SysDefineExtensionValue.LoadResultByTableNameAndRefrenceId(_onlyTableName, send.RecId, currentUserId); BaseSendEntity bse = new BaseSendEntity() { SendKey = JsonConvert.SerializeObject(data), SendPair = extend.Any() ? JsonConvert.SerializeObject(extend) : null }; OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(bse) }; return JsonConvert.SerializeObject(oe); } public static string DeleteResult(string currentUserId, string currentClientId, string currentSendParameter) { using (var tranScope = new TransactionScope()) { string connectionString = AppHelper.GetLinkString(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { JArray listSendID = (JArray)JsonConvert.DeserializeObject(currentSendParameter); foreach (var send in listSendID) { string value = (((Newtonsoft.Json.Linq.JValue)(send))).Value.ToString(); MySqlParameter paramRecId = new MySqlParameter("@RecId", MySqlDbType.VarChar) { Value = value }; MysqlHelper.ExecuteNonQuery(connection, "update TestDotPart set IsActive =0 where RecId=@RecId", paramRecId); //更新池 TestdotpartPool.Delete(value); //save log InsertToLog(connection, value, currentUserId, ((int)AppEnum.OperType.Delete).ToString()); } tranScope.Complete(); OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(true) }; return JsonConvert.SerializeObject(oe); } } } public static string LoadLogResults(string currentUserId, string currentClientId, string currentSendParameter) { PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity; List ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List)) as List; string keyword1 = GetConditionValue(ce, "KeyWord1"); string keyword2 = GetConditionValue(ce, "KeyWord2"); string dateFrom = GetConditionValue(ce, "DateFrom"); string dateTo = GetConditionValue(ce, "DateTo"); DateTime dtStart; DateTime dtEnd; if (!DateTime.TryParse(dateFrom, out dtStart)) dtStart = new DateTime(1900, 1, 1); if (!DateTime.TryParse(dateTo, out dtEnd)) dtEnd = new DateTime(2100, 1, 1); int count = 0; string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2); DataTable dt = MysqlHelper.GetPager(out count, _log_fields, _log_tableName, fromwhere, se.OrderBy, se.SortOrder, se.PageIndex, se.Rows); List rows = MysqlHelper.DataTableToIList(dt) as List; foreach (var row in rows) { ConvertSingleData(row); } RecordEntity result = new RecordEntity(); result.total = (int)Math.Ceiling(count * 1.0 / se.Rows); result.page = se.PageIndex; result.records = se.PageIndex; result.rows = rows; OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(result) }; return JsonConvert.SerializeObject(oe); } public static string ExportResults(string currentUserId, string currentClientId, string currentSendParameter) { PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity; List ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List)) as List; string keyword1 = GetConditionValue(ce, "KeyWord1"); string keyword2 = GetConditionValue(ce, "KeyWord2"); string dateFrom = GetConditionValue(ce, "DateFrom"); string dateTo = GetConditionValue(ce, "DateTo"); DateTime dtStart; DateTime dtEnd; if (!DateTime.TryParse(dateFrom, out dtStart)) dtStart = new DateTime(1900, 1, 1); if (!DateTime.TryParse(dateTo, out dtEnd)) dtEnd = new DateTime(2100, 1, 1); string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2); string sqlStr = string.Format("select {0} from {1} where 1=1 {2}", _fields, _tableName, fromwhere); DataTable dt = MysqlHelper.ExecuteDataTable(sqlStr); List datas = MysqlHelper.DataTableToIList(dt) as List; HSSFWorkbook hssfworkbook = new HSSFWorkbook(); ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); IRow row0 = sheet1.CreateRow(0); int colIndex = 0; row0.CreateCell(colIndex).SetCellValue("RecId"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartCode"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartName"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartType"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartIndex"); colIndex++; row0.CreateCell(colIndex).SetCellValue("CreateBy"); colIndex++; row0.CreateCell(colIndex).SetCellValue("CreateTime"); colIndex++; row0.CreateCell(colIndex).SetCellValue("ModifyBy"); colIndex++; row0.CreateCell(colIndex).SetCellValue("ModifyTime"); colIndex++; row0.CreateCell(colIndex).SetCellValue("IsActive"); colIndex++; int rowIndex = 1; foreach (var data in datas) { IRow row = sheet1.CreateRow(rowIndex); colIndex = 0; row.CreateCell(colIndex).SetCellValue(data.RecId); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartCode); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartName); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartType); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartIndex.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.CreateBy); colIndex++; row.CreateCell(colIndex).SetCellValue(data.CreateTime.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.ModifyBy); colIndex++; row.CreateCell(colIndex).SetCellValue(data.ModifyTime.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.IsActive.ToString()); colIndex++; rowIndex++; } //set range border AppHelper.SetBorder(hssfworkbook, 0, rowIndex, colIndex); string fileName = string.Format("{0}.xls", Guid.NewGuid()); string xlsPath = string.Format("{0}\\{1}", SystemConst.CurrentExcelPath, fileName); FileStream fs = new FileStream(xlsPath, FileMode.CreateNew, FileAccess.Write); hssfworkbook.Write(fs); fs.Close(); OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(string.Format("{0}{1}", SystemConst.CurrentExcelLinkFolder, fileName)) }; return JsonConvert.SerializeObject(oe); } public static string ExportLogResults(string currentUserId, string currentClientId, string currentSendParameter) { PagerEntity se = JsonConvert.DeserializeObject(currentSendParameter, typeof(PagerEntity)) as PagerEntity; List ce = JsonConvert.DeserializeObject(se.Conditions, typeof(List)) as List; string keyword1 = GetConditionValue(ce, "KeyWord1"); string keyword2 = GetConditionValue(ce, "KeyWord2"); string dateFrom = GetConditionValue(ce, "DateFrom"); string dateTo = GetConditionValue(ce, "DateTo"); DateTime dtStart; DateTime dtEnd; if (!DateTime.TryParse(dateFrom, out dtStart)) dtStart = new DateTime(1900, 1, 1); if (!DateTime.TryParse(dateTo, out dtEnd)) dtEnd = new DateTime(2100, 1, 1); string fromwhere = QueryFromWhere(dateFrom, dtStart, dtEnd, keyword1, keyword2); string sqlStr = string.Format("select {0} from {1} where 1=1 {2}", _log_fields, _log_tableName, fromwhere); DataTable dt = MysqlHelper.ExecuteDataTable(sqlStr); List datas = MysqlHelper.DataTableToIList(dt) as List; HSSFWorkbook hssfworkbook = new HSSFWorkbook(); ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); IRow row0 = sheet1.CreateRow(0); int colIndex = 0; row0.CreateCell(colIndex).SetCellValue("LogId"); colIndex++; row0.CreateCell(colIndex).SetCellValue("RecId"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartCode"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartName"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartType"); colIndex++; row0.CreateCell(colIndex).SetCellValue("PartIndex"); colIndex++; row0.CreateCell(colIndex).SetCellValue("CreateBy"); colIndex++; row0.CreateCell(colIndex).SetCellValue("CreateTime"); colIndex++; row0.CreateCell(colIndex).SetCellValue("ModifyBy"); colIndex++; row0.CreateCell(colIndex).SetCellValue("ModifyTime"); colIndex++; row0.CreateCell(colIndex).SetCellValue("IsActive"); colIndex++; row0.CreateCell(colIndex).SetCellValue("OperTime"); colIndex++; row0.CreateCell(colIndex).SetCellValue("OperUser"); colIndex++; row0.CreateCell(colIndex).SetCellValue("OperType"); colIndex++; row0.CreateCell(colIndex).SetCellValue("OperInfo"); colIndex++; int rowIndex = 1; foreach (var data in datas) { IRow row = sheet1.CreateRow(rowIndex); colIndex = 0; row.CreateCell(colIndex).SetCellValue(data.LogId); colIndex++; row.CreateCell(colIndex).SetCellValue(data.RecId); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartCode); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartName); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartType); colIndex++; row.CreateCell(colIndex).SetCellValue(data.PartIndex.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.CreateBy); colIndex++; row.CreateCell(colIndex).SetCellValue(data.CreateTime.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.ModifyBy); colIndex++; row.CreateCell(colIndex).SetCellValue(data.ModifyTime.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.IsActive.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.OperTime.ToString()); colIndex++; row.CreateCell(colIndex).SetCellValue(data.OperUser); colIndex++; row.CreateCell(colIndex).SetCellValue(data.OperType); colIndex++; row.CreateCell(colIndex).SetCellValue(data.OperInfo); colIndex++; rowIndex++; } //set range border AppHelper.SetBorder(hssfworkbook, 0, rowIndex, colIndex); string fileName = string.Format("{0}.xls", Guid.NewGuid()); string xlsPath = string.Format("{0}\\{1}", SystemConst.CurrentExcelPath, fileName); FileStream fs = new FileStream(xlsPath, FileMode.CreateNew, FileAccess.Write); hssfworkbook.Write(fs); fs.Close(); OutEntity oe = new OutEntity() { ErrorCode = ErrorCode.Success, ReturnObj = JsonConvert.SerializeObject(string.Format("{0}{1}", SystemConst.CurrentExcelLinkFolder, fileName)) }; return JsonConvert.SerializeObject(oe); } private static string DealCellValue(IRow row, int index) { if (row.GetCell(index) == null) return ""; return row.GetCell(index).CellType == CellType.Formula ? row.GetCell(index).StringCellValue : row.GetCell(index).ToString(); } } }