识别Excel单元格中的删除线文本 [英] Identify strikethrough text in excel cell

查看:517
本文介绍了识别Excel单元格中的删除线文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

美好的一天!

我的问题是excel文本中只有一部分是删除线,我想删除所说的删除线文本

例如:

敏捷的棕色狐狸跳过了懒狗

可以说:棕色字是删除线.我该如何删除所说的单词,并仅返回不在删除线中的那些单词.

谢谢
Chaegie

Hi Everyone,

good day!

My problem is only portion of the excel text is Strikethrough i want to delete the said Strikethrough text

example:

The quick brown fox jump over the lazy dog

let say: brown word is Strikethrough. how can i delete the said word, and return only those words were not in Strikethrough.

Thanks
Chaegie

推荐答案

让我们假设您的文本棕色狐狸跳过懒狗"位于excel工作表的第一个单元格中,表示单元格( 1,1),单词"brown"是StrikeThrough

然后制作一个按钮或一个宏,并对其进行如下

子Button1_Click()
Dim str As String
对于i = 1到Len(Cells(1,1))
如果Cells(1,1).Characters(i,1).Font.Strikethrough = False,则str = str + Cells(1,1).Characters(i,1).Text
接下来我
单元格(10,1)= str
结束子

这会将不含单词"brown"的短语复制到单元格(10,1)
但是请注意,这将使该短语在"quick"和"fox"之间留下两个空格,因为代码仅删除了"brown"一词,并在其前后留了空格.

我将保留代码的调整功能,以为您消除多余的空间:)

您可以在此处下载示例:

http://rapidshare.com/files/330791064/Book1.xls [
Let''s suppose that your text "The quick brown fox jump over the lazy dog" is in the first cell of the excel sheet, means cell (1,1), with the word "brown" is StrikeThrough

Then make a button or a macro and edit it to be as follows:

Sub Button1_Click()
Dim str As String
For i = 1 To Len(Cells(1, 1))
If Cells(1, 1).Characters(i, 1).Font.Strikethrough = False Then str = str + Cells(1, 1).Characters(i, 1).Text
Next i
Cells(10, 1) = str
End Sub

This will copy the phrase without the word "brown" to the cell (10,1)
But be alerted, this will leave the phrase with two spaces between the words "quick" and "fox" because the code deleted the word "brown" only, and left the spaces before and after it.

I shall leave the tuning of the code to remove the extra space for you :)

you can download the example here:

http://rapidshare.com/files/330791064/Book1.xls[^]


Chaegie,
不确定,但我认为您正在寻找C#解决方案.这是又快又脏的东西.我敢肯定,有一个更雄辩的解决方案,但这可行.而且,至少,它显示了C#语法来检测删除线作为您的起点.

使用Microsoft.Office.Core;
使用Excel = Microsoft.Office.Interop.Excel;

...
...

//打开一些工作簿并获取电子表格...
Excel.Application app =新的Excel.Application();

Excel.Workbook工作簿= app.Workbooks.Open(工作簿名称",
Type.Missing,Type.Missing,
Type.Missing,Type.Missing,
Type.Missing,Type.Missing,
Type.Missing,Type.Missing,
Type.Missing,Type.Missing,
Type.Missing,Type.Missing,
Type.Missing,Type.Missing);

//从工作簿中获取工作表
Excel.Worksheet ws =(Excel.Worksheet)app.Sheets ["worksheet name"];

...
...

//将范围设置为等于单个单元格的范围,该单元格的文本需要清除
Excel.Range范围= worksheet.get_Range("somecell","somecell");

字符串clean_string = CleanStrikethroughChars(Excel.Range range);
//现在您可以将单元格值替换为clean_string值
...
...

字符串CleanStrikethroughChars(Excel.Range range)
{
字符串s =";
int char_index = 1;
int长度= range.Cells.Value2.ToString().Length;

while(char_index< length)
{
//只继续使用不是删除线的任何char构建字符串
if(!(bool)range.Cells.get_Characters(char_index,1).Font.Strikethrough;)
s + = range.Cells.get_Characters(char_index,1).Text;

char_index ++;
}
return s;
}
Hi Chaegie,
Not sure but I thought you were looking for a C# solution. Here is something quick and dirty. I''m sure there is a more eloquent solution but this works. And, at the very least, it shows the C# syntax to detect strike-through as a starting point for you.

using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

...
...

//open some workbook and get a spreadsheet...
Excel.Application app = new Excel.Application();

Excel.Workbook workbook = app.Workbooks.Open("workbook name",
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing);

//get the worksheet from the workbook
Excel.Worksheet ws = (Excel.Worksheet)app.Sheets["worksheet name"];

...
...

//set a range equal to a single cell that has your text that needs cleaned
Excel.Range range = worksheet.get_Range("somecell", "somecell");

string clean_string = CleanStrikethroughChars(Excel.Range range);
//now you can replace the cell value with the clean_string value
...
...

string CleanStrikethroughChars(Excel.Range range)
{
string s="";
int char_index = 1;
int length = range.Cells.Value2.ToString().Length;

while(char_index<length)
{
//just keep building the string with any char that is not strikethrough
if(!(bool)range.Cells.get_Characters(char_index,1).Font.Strikethrough;)
s+=range.Cells.get_Characters(char_index,1).Text;

char_index++;
}
return s;
}


这篇关于识别Excel单元格中的删除线文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆