public object ValueFomatted(object source, DataCellType dataFormater) { if (DataSouceType != null && ForzenColumn != null) { List <string> s = new List <string>(); IList DataSourceValue = GetList(DataSouceType) as IList; foreach (DataGridViewRow row in this.Rows) { if (this[ForzenColumn.Index, row.Index].Value != null) { object Entity = System.Activator.CreateInstance(DataSouceType); foreach (DataGridViewColumn column in this.Columns) { string DataPropertyName = column.DataPropertyName; if (!string.IsNullOrEmpty(DataPropertyName)) { PropertyInfo propertyInfo = Entity.GetType().GetProperty(DataPropertyName); if (propertyInfo != null) { if (propertyInfo.PropertyType == typeof(int)) { propertyInfo.SetValue(Entity, TryParse.StrToInt(this[column.Index, row.Index].Value), null); } else if (propertyInfo.PropertyType == typeof(decimal)) { propertyInfo.SetValue(Entity, TryParse.StrToDecimal(this[column.Index, row.Index].Value), null); } else { propertyInfo.SetValue(Entity, this[column.Index, row.Index].Value, null); } } } } DataSourceValue.Add(Entity); } else { break; } } return(DataSourceValue); } return(null); }
protected override void OnCellEndEdit(DataGridViewCellEventArgs e) { base.OnCellEndEdit(e); if (ForzenColumn == null) { return; } if (this[e.ColumnIndex, e.RowIndex].Value == null) { return; } if (string.IsNullOrEmpty(TryParse.ToString(this[e.ColumnIndex, e.RowIndex].Value))) { return; } if (string.IsNullOrEmpty(TryParse.ToString(this[ForzenColumn.Index, e.RowIndex].Value))) { int NextValue = 1; if (e.RowIndex > 0) { NextValue = TryParse.StrToInt(this[ForzenColumn.Index, e.RowIndex - 1].Value) + 1; } string Value = ("000000000" + NextValue); this[ForzenColumn.Index, e.RowIndex].Value = Value.Substring(Value.Length - ForzenColumnLength); } if (ValidRowIndex.Contains(e.RowIndex)) { ValidRowIndex.Remove(e.RowIndex); } if (_IsNoNullEmptyColumnIndex != null && _IsNoNullEmptyColumnIndex.Count > 0 && ForzenColumn != null) { foreach (int columnIndex in _IsNoNullEmptyColumnIndex) { object value = this[columnIndex, e.RowIndex].Value; if (value != null && !ValidRowIndex.Contains(e.RowIndex)) { ValidRowIndex.Add(e.RowIndex); } } } }
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) { if (this.DataGridView != null && this.ColumnIndex >= 0) { if (this.DataGridView.Columns[this.ColumnIndex] is DataGridViewForzenColumn) { DataGridViewForzenColumn ForzenColumn = this.DataGridView.Columns[this.ColumnIndex] as DataGridViewForzenColumn; if (ForzenColumn.IsAutoValue) { int NextValue = 1; if (rowIndex > 0) { NextValue = TryParse.StrToInt(this.DataGridView[this.ColumnIndex, rowIndex - 1].FormattedValue) + 1; } string returnValue = ("000000000" + NextValue); value = returnValue.Substring(returnValue.Length - ForzenColumn.ForzenColumnLength); this.SetValue(rowIndex, value); } } } return(base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context)); }
private void toolGo_Click(object sender, EventArgs e) { int PageIndex = TryParse.StrToInt(txtPageIndex.Text, 0); if (PageIndex <= 0 || PageIndex > this.PageCount) { MessageBoxHelper.ShowError("非法页数,请填写正确的页数"); return; } if (dataPager != null) { if (dataPager.TempSerachData == null) { dataPager.TempSerachData = dataPager.SerachEntity.Clone(); } CurrentPageIndex = TryParse.StrToInt(txtPageIndex.Text) - 1; dataPager.TempSerachData.RowCount = CurrentPageIndex * TryParse.StrToInt(dataPager.TempSerachData.TopCount); SerachData serachData = dataPager.TempSerachData.Clone(); dataPager.OnBindData(ref serachData, false); this.TotalCount = serachData.TotalCount; SetPagerInfo(this.TotalCount, TryParse.StrToInt(dataPager.TempSerachData.TopCount), false); } }
private void ExcuteExport() { this.ThreadState = EnumGloabParas.EnumThreadState.Run; int totalCount = this.m_DataTable.Rows.Count; int rowRead = 0; bool IsCompleted = false; string errMsg = string.Empty; DbConnection conn = GetConnection(this.ConnectionString); if (conn == null) { this.ThreadState = EnumGloabParas.EnumThreadState.SpiderCompleted; errMsg = "无法获取到数据库连接"; ExportCompleted(totalCount, errMsg, IsCompleted); return; } if (e_ExportStartingEvent != null) { e_ExportStartingEvent(this, new cExportStartEventArgs() { TotalCount = totalCount, }); } try { conn.Open(); foreach (DataRow item in this.m_DataTable.Rows) { string commandText = this.CommandText; foreach (DataColumn column in m_DataTable.Columns) { string replaceStr = "{" + column.ColumnName + "}"; if (commandText.IndexOf(replaceStr) != -1) { string rowValue = TryParse.ToString(item[column.ColumnName]); rowValue = rowValue.Replace("'", "''"); commandText = commandText.Replace(replaceStr, rowValue); } } DbCommand command = conn.CreateCommand(); command.CommandText = commandText; int queryCount = command.ExecuteNonQuery(); rowRead++; string m = ((100m * rowRead) / totalCount).ToString(); int percent = TryParse.StrToInt(m); if (e_ExportProgressingEvent != null) { e_ExportProgressingEvent(this, new ExportProgressingArgs() { ExportCount = rowRead, ExportPercent = percent, TotalCount = totalCount, }); } } this.ThreadState = EnumGloabParas.EnumThreadState.SpiderCompleted; IsCompleted = true; } catch (Exception ex) { this.ThreadState = EnumGloabParas.EnumThreadState.SpiderCompleted; IsCompleted = false; errMsg = ex.Message + ex.Source; } finally { if (conn != null && conn.State != ConnectionState.Closed) { conn.Close(); } ExportCompleted(totalCount, errMsg, IsCompleted); } }
public void ExportToTxt() { if (false == ValidExport()) { return; } int totalCount = m_DataTable.Rows.Count; string errMsg = string.Empty; int rowRead = 0; int percent = 0; bool isCompleted = false; if (e_ExportStartingEvent != null) { e_ExportStartingEvent(this, new cExportStartEventArgs() { TotalCount = totalCount }); } try { //写入标题 using (System.IO.StreamWriter sw = new System.IO.StreamWriter(this.ConnectionString, false, Encoding.UTF8)) { for (int i = 0; i < m_DataTable.Rows.Count; i++) { foreach (DataColumn item in m_DataTable.Columns) { string value = m_DataTable.Rows[i][item].ToString(); value = Regex.Replace(value, @"\s", ""); sw.Write(value + "\t"); } sw.WriteLine(); rowRead++; string m = ((100m * rowRead) / totalCount).ToString(); percent = TryParse.StrToInt(m); if (e_ExportProgressingEvent != null) { e_ExportProgressingEvent(this, new ExportProgressingArgs() { ExportCount = rowRead, ExportPercent = percent, TotalCount = totalCount, }); } } sw.Close(); isCompleted = true; } } catch (Exception ex) { errMsg = ex.Message; isCompleted = false; } finally { if (e_ExportEndedEvent != null) { e_ExportEndedEvent(this, new cExportEndedEventArgs() { IsCompleted = isCompleted, TotalCount = totalCount, Message = errMsg, }); } } }
/// <summary> /// 将DataTable导出为Excel文件(.xls) /// </summary> /// <param name="dt">要导出的DataTable</param> public void ExportToExcel() { if (false == ValidExport()) { return; } this.ThreadState = EnumGloabParas.EnumThreadState.Run; Microsoft.Office.Interop.Excel.Application xlApp = null; Microsoft.Office.Interop.Excel.Workbooks workbooks = null; int rowTempRead = 0; int sheetCount = 1; int rangeColumn = 0; object rangeValue = ""; int r = 0; bool isComplete = false; string errMsg = string.Empty; int totalCount = 0; try { xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { if (e_ExportEndedEvent != null) { e_ExportEndedEvent(this, new cExportEndedEventArgs() { IsCompleted = false, TotalCount = 0, Message = "无法创建Excel对象,可能您的电脑未安装Excel", }); } return; } workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 //Microsoft.Office.Interop.Excel.Range range = null; totalCount = m_DataTable.Rows.Count; int rowRead = 0; int percent = 0; if (e_ExportStartingEvent != null) { e_ExportStartingEvent(this, new cExportStartEventArgs() { TotalCount = totalCount }); } //写入标题 WriteTitle(worksheet); //写入内容 for (r = 0; r < m_DataTable.DefaultView.Count; r++, rangeColumn++) { if (rowTempRead > 60000) { worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); WriteTitle(worksheet); sheetCount++; rowTempRead = 0; rangeColumn = 0; } for (int i = 0; i < m_DataTable.Columns.Count; i++) { rangeValue = m_DataTable.DefaultView[r][i]; rangeValue = "'" + rangeValue; worksheet.Cells[rangeColumn + 2, i + 1] = rangeValue.ToString(); //range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1]; //range.Font.Size = 9;//字体大小 //加边框 //range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //range.EntireColumn.AutoFit();//自动调整列宽 } rowRead++; rowTempRead++; string m = ((100m * rowRead) / totalCount).ToString(); percent = TryParse.StrToInt(m); if (e_ExportProgressingEvent != null) { e_ExportProgressingEvent(this, new ExportProgressingArgs() { ExportCount = rowRead, ExportPercent = percent, TotalCount = totalCount, }); } } //range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin; //if (m_DataTable.Columns.Count > 1) //{ // range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin; //} workbook.Saved = true; workbook.SaveCopyAs(ConnectionString); isComplete = true; } catch (Exception ex) { errMsg = ex.Message + "rows:" + r + "rangeValue:" + rangeValue; DMSFrame.Loggers.LoggerManager.FileLogger.LogWithTime(errMsg); } finally { if (workbooks != null) { workbooks.Close(); } if (xlApp != null) { xlApp.Workbooks.Close(); xlApp.Quit(); int generation = System.GC.GetGeneration(xlApp); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; System.GC.Collect(generation); } GC.Collect();//强行销毁 #region 强行杀死最近打开的Excel进程 KillExcel(); if (e_ExportEndedEvent != null) { e_ExportEndedEvent(this, new cExportEndedEventArgs() { IsCompleted = isComplete, TotalCount = totalCount, Message = errMsg, }); } #endregion } }