XAF 如何从Excel复制多个单元格内容到GridView(收藏)
XAF 如何从Excel复制多个单元格内容到GridView
2012年04月11日 ⁄ 综合 ⁄ 共 10998字 ⁄ 字号 小 中 大 ⁄ 评论关闭 how to paste some excel content to xtragrid? .相關資料 http://community.devexpress.com/forums/t/36684.aspx http://community.devexpress.com/forums/t/58611.aspx .調用方法: using System;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.XtraGrid.Views.Grid; namespace XafExpandExtendTest.Module
{
public partial class CopyPasteListViewController : ViewController
{
public CopyPasteListViewController()
{
InitializeComponent();
RegisterActions(components);
TargetViewType = ViewType.ListView;
} private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e)
{
ListView lv = View as ListView;
GridView gv = (lv.Editor as GridListEditor).GridView;
XtraGridHelper.GridViewClipboardPaste(gv);
}
}
} .實現代碼: using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
namespace XafExpandExtendTest.Module
{
public class XtraGridHelper
{
private static string ClipboardText
{
get
{
string value = string.Empty;
IDataObject iData = Clipboard.GetDataObject();
if (iData != null)
{
if (iData.GetDataPresent(DataFormats.Text))
{
value = iData.GetData(DataFormats.Text) as string;
}
}
return value;
}
set
{
Clipboard.SetDataObject(value);
}
}
//private static string[] ClipboardTextLines
//{
// get
// {
// return XtraGridHelper.ClipboardText.Split(' ');
// }
//} public static void PasteTable(GridView gridView)
{
gridView.ClearSorting();
int originalFocusedVisibleColIndex = gridView.FocusedColumn.VisibleIndex;
string[] clipboardTextLines = XtraGridHelper.ClipboardTextLines; // paste data from clipboard into row cells
foreach (string line in clipboardTextLines)
{
if (line != string.Empty)
{
string[] lineFragments = line.Split('\t');
foreach (string lineFragment in lineFragments)
{
// 'paste' in new value
gridView.ShowEditor();
if (gridView.ActiveEditor != null)
{
gridView.ActiveEditor.Text = lineFragment.Trim();
gridView.CloseEditor();
} // move to next visible column if next visible column exists
if (gridView.FocusedColumn.VisibleIndex < gridView.VisibleColumns.Count - )
{
gridView.FocusedColumn = gridView.VisibleColumns[gridView.FocusedColumn.VisibleIndex + ];
}
else
{
break; //stop 'pasting' in focused row
}
}
// move to next row
if (gridView.FocusedRowHandle < gridView.RowCount - )
{
gridView.MoveNext();
gridView.FocusedColumn = gridView.VisibleColumns[originalFocusedVisibleColIndex];
}
else
{
break; //stop 'pasting' in the grid
}
}
}
} /*You might consider pointing to kb articles that have the same paste functionality;
* the article refrenced by Brendon adds rows when a paste action is invoked.
* The solution presented in this thread pastes values into existing cells, relative to the focused cell.
* And man do I hate when I see questions posted with no answers, like the one that started this thread.
* Here is an update to my solution above, it only accomidates the column and edit types in my grid...
*/
public static void GridViewClipboardPaste(GridView gridView)
{
gridView.ClearSorting();
int originalFocusedVisibleColIndex = gridView.FocusedColumn.VisibleIndex;
string[] clipboardTextLines = XtraGridHelper.ClipboardTextLines; gridView.BeginUpdate(); //lock grid for update
// paste data from clipboard into row cells
foreach (string line in clipboardTextLines)
{
if (line != string.Empty)
{
string[] lineFragments = line.Split('\t');
foreach (string lineFragment in lineFragments)
{
string clipboardString = lineFragment.Trim(); // 'paste' in new value
try
{
RepositoryItem edit = gridView.FocusedColumn.ColumnEdit;
if (edit != null)
{
switch (edit.EditorTypeName)
{
case "ImageComboBoxEdit":
RepositoryItemImageComboBox imageComboBoxEdit = (RepositoryItemImageComboBox)edit;
foreach (ImageComboBoxItem item in imageComboBoxEdit.Items)
{
if (item.Description.Equals(clipboardString))
{
gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.FocusedColumn, item.Value);
break;
}
}
break;
case "CheckEdit":
bool checkValue;
if (Boolean.TryParse(clipboardString, out checkValue))
{
gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.FocusedColumn, checkValue);
}
break;
}
}
else
{
object newValue = null;
switch (gridView.FocusedColumn.ColumnType.Name)
{
case "String":
newValue = clipboardString;
break;
case "Boolean":
newValue = Boolean.Parse(clipboardString);
break;
case "Int32":
newValue = Int32.Parse(clipboardString);
break;
}
gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.FocusedColumn, newValue);
}
}
catch (Exception ex)
{
//do nothing
} // move to next visible column if next visible column exists
if (gridView.FocusedColumn.VisibleIndex < gridView.VisibleColumns.Count - )
{
gridView.FocusedColumn = gridView.VisibleColumns[gridView.FocusedColumn.VisibleIndex + ];
}
else
{
break; //stop 'pasting' in focused row
}
}
// move to next row
if (gridView.FocusedRowHandle < gridView.RowCount - )
{
gridView.MoveNext();
gridView.FocusedColumn = gridView.VisibleColumns[originalFocusedVisibleColIndex];
}
else
{
break; //stop 'pasting' in the grid
}
}
}
gridView.EndUpdate(); //lock grid for update
}
////////////////////////////////////////////////////////////////////
private static string[] ClipboardTextLines
{
get
{
var clipboardText = ClipboardText;
if (clipboardText.Contains(Environment.NewLine))
{
return clipboardText.Trim().Replace(Environment.NewLine, "\n") .Split('\n');
// Replace CRLF with just CR then Split.
} return clipboardText.Split(' ');
}
} // And this is what I did to speed it up:
public static void PasteTable2(GridView gridView)
{
gridView.ClearSorting();
var clipboardTextLines = new List<string>(ClipboardTextLines);
// If we have too many rows trim the list.
if (gridView.RowCount < clipboardTextLines.Count)
clipboardTextLines = new List<string>(clipboardTextLines.GetRange(, gridView.RowCount));
var currentCol = gridView.FocusedColumn;
var numberOfColumnsThatCanBePastedTo = gridView.VisibleColumns.Count - currentCol.VisibleIndex;
var pasteList = new List<List<string>>();
foreach (var line in clipboardTextLines)
{
var rowValues = new List<string>(line.Split('\t'));
// Make sure we don't overshoot the columns
if(rowValues.Count > numberOfColumnsThatCanBePastedTo)
rowValues = new List<string>(rowValues.GetRange(, numberOfColumnsThatCanBePastedTo));
pasteList.Add(rowValues);
}
var currentRow = gridView.FocusedRowHandle;
for (int i = ; i < pasteList.Count; i++)
{
var pasteRow = currentRow + i;
for (int j = ; j < pasteList[i].Count; j++)
{
var pasteCol = currentCol.VisibleIndex + j;
gridView.SetRowCellValue(pasteRow, gridView.VisibleColumns[pasteCol], pasteList[i][j]);
}
}
}
}
}
XAF 如何从Excel复制多个单元格内容到GridView(收藏)的更多相关文章
- 使用poi导出Excel,并设定单元格内容类型,抛出异常
本例子使用的是HSSF,为Excel2003提供处理方案. 设定为输入类型为数值 import org.apache.poi.hssf.usermodel.DVConstraint; import o ...
- excel VBA把一个单元格内容按逗号拆分并依次替换到另一个单元格的括号里面(本题例子,把文本中的括号换成{答案}的格式,并按顺序填空)
方法1:运用excel单元格拆分合并实现 思路:用VBA正则查询左侧括号个数,对右侧单元格逐一按逗号.顿号等符号分列,同时左侧按括号分列(分列只能按括号单边分列),分列完成后按要求合并,本题事例把括号 ...
- excel用函数去掉单元格内容中的括号,并只保留单元格里面的内容
1.substitute(需要执行替换操作的单元格,需要替换的字符,替换后的字符,有多个需要替换的字符可以指定替换的第几个) 例如:aab--substitute("aab",&q ...
- C# 获取Excel中的合并单元格
C# 获取Excel中的合并单元格 我们在制作表格时,有时经常需要合并及取消合并一些单元格.在取消合并单元格时需要逐个查找及取消,比较麻烦.这里分享一个简单的方法来识别Excel中的合并单元格,识别这 ...
- 读取Excel文件中的单元格的内容和颜色
怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...
- Excel单元格内容太多会覆盖遮住下一单元格范围
Excel单元格内容太多会覆盖遮住下一单元格范围分步阅读 Excel中的单元格内容,有着不同的对齐方式.用户可根据自己的需求,在处理数据的时候,自行设置所需要的对齐方式. 当您在处理数据的时候,如果设 ...
- poi 升级至4.x 的问题总结(POI Excel 单元格内容类型判断并取值)
POI Excel 单元格内容类型判断并取值 以前用 cell.getCachedFormulaResultType() 得到 type 升级到4后获取不到了 换为:cell.getCellType( ...
- Excel表格如何保护单元格不被修改
Excel如何保护单元格不被修改 有时使用Excel时希望保护单元格不被修改,这可以叫做单元格的“写保护”即把光标定位在一个不允许输入数据的区域内时,是无论如何也无法在里面输入数据的.下面咱们就一起 ...
- excel 单元格内容太多,替换有问题
excel 单元格内容太多,替换有问题
随机推荐
- 喵神 onevcat 的直播首秀
喵神 onevcat 的直播首秀 王巍在圈内人称喵神,我和他在网上很早就认识,平时多有交流.在我眼中,他是一个幽默风趣高手.虽然他的博客中主要内容是 iOS 开发,但是他实际上涉及的技术领域还包括 ...
- jquery $.each 和for 怎么跳出循环(终止本次循环)
1.for循环中我们使用continue:终止本次循环计入下一个循环,使用break终止整个循环. 2.而在jquery中 $.each则对应的使用return true 和return false ...
- js 控制不同客户端 访问不同CSS js
function loadCSS(flag) { var t='.css'; if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios ...
- mysql 循环insert
亲测成功!可用,复制即可 DELIMITER ;; CREATE PROCEDURE test_insert() BEGIN DECLARE y TINYINT DEFAULT 1;WHILE y&l ...
- HashSet非常的消耗空间,TreeSet因为有排序功能,因此资源消耗非常的高,我们应该尽量少使用
注:HashMap底层也是用数组,HashSet底层实际上也是HashMap,HashSet类中有HashMap属性(我们如何在API中查属性).HashSet实际上为(key.null)类型的Has ...
- 关系运算符:instanceof
关系运算符:instanceof a instanceof Animal;(这个式子的结果是一个布尔表达式) a为对象变量,Animal是类名. 上面语句是判定a是否可以贴Animal标签.如果可以贴 ...
- maven+springmvc错误 JAX-RS (REST Web Services) 2.0 can not be installed
项目problem提示错误 JAX-RS (REST Web Services) 2.0 can not be installed : One or more constraints have not ...
- CSS伪类选择器 - nth-child(an+b):
CSS伪类选择器 - nth-child(an+b): 第一种:简单数字序号写法:nth-child(number)直接匹配第number个元素.参数number必须为大于0的整数.li:nth-ch ...
- Python+selenium之简单介绍unittest单元测试框架
Python+selenium之简单介绍unittest单元测试框架 一.unittest简单介绍 unittest支持测试自动化,共享测试用例中的初始化和关闭退出代码,在unittest中最小单元是 ...
- Raw-OS源代码分析之idle任务
分析的内核版本号截止到2014-04-15,基于1.05正式版,blogs会及时跟进最新版本号的内核开发进度,若源代码凝视出现"???"字样.则是未深究理解部分. Raw-OS官方 ...