需要使用interop从单词表中获取项目符号以进行excel操作
本文关键字:符号 项目 操作 excel 获取 interop 单词表 | 更新日期: 2023-09-27 18:26:28
所以我一直在努力学习如何使用Interop(以及一般编程)。基本上,我的Word文档中有这些表,需要导出到Excel。
我已经得到了要导出到Excel的基本文本,但我在Word中的表格有相当多的格式,主要是一些单元格中的项目符号列表,我需要保留或重建这些单元格,一旦我将它们导出到Excel中,可能会吗??
这是我的密码。这显然导出了文本。通过一些研究,我能够获得子弹和凹痕,并在richtextbox中看到它们(参见注释代码)。我需要这个一个接一个地工作。我无法在数组中构建所有内容并将其传递给Excel。
这是我尝试更新的应用程序中的一个约束。这里的代码只是一个测试用例。我的问题是,如何在excel中导出或重建这些内容?
我找到了一些关于这个主题的文章,但没有什么能帮助我度过难关,这就是我在这里提问的原因。如有任何帮助,我们将不胜感激。
private void btnExecute_Click(object sender, EventArgs e)
{
var word = new Microsoft.Office.Interop.Word.Application();
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelworkbook = excelApp.Workbooks.Open("D:''mytest.xlsx");
Excel._Worksheet excelworkSheet = (Excel.Worksheet)excelApp.ActiveSheet;
excelApp.Visible = true;
object miss = System.Reflection.Missing.Value;
object path = txbxFilePath1.Text;
object readOnly = true;
var docs = word.Documents.Open(ref path, ref miss, ref readOnly,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss);
foreach (Table tb in docs.Tables)
{
for (int row = 1; row <= tb.Rows.Count; row++)
{
for (int mycols = 1; mycols <= tb.Columns.Count; mycols++)
{
var cells = tb.Cell(row, mycols).Range.Paragraphs;
foreach (Paragraph para in cells)
{
//string bulltxt = para.Range.Text;
//string leftIndent = para.LeftIndent.ToString();
//string bulletStr = para.Range.ListFormat.ListString;
//rTxBxResult.AppendText(leftIndent + "'t" + bulletStr + "'t" + para.Range.Text);
}
excelworkSheet.Cells[row, mycols] = tb.Cell(row, mycols).Range.Text;
}
}
}
}
因此,在一位朋友的帮助下,我们想出了一个"a"解决方案。基本上,我已经创建了一个自定义类,并使用它,而不是试图通过interop来完成整个工作,我发现这是有限制的。结尾有一个小错误,我用"*"answers"-"作为我的子弹头角色。有一种方法可以使用某种应用程序发送密钥的方法将密钥发送到excel(https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.sendkeys(v=office.11).aspx)但是我今天就是没有时间。
我在代码中添加了注释以提供帮助。希望有人觉得这个有用。
private void btnExecute_Click(object sender, EventArgs e)
{
var word = new Microsoft.Office.Interop.Word.Application();
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelworkbook = excelApp.Workbooks.Open("D:''myxlspreadsheet");
Excel._Worksheet excelworkSheet = (Excel.Worksheet)excelApp.ActiveSheet;
excelApp.Visible = true;
object miss = System.Reflection.Missing.Value;
object path = txbxFilePath1.Text;
object readOnly = true;
var docs = word.Documents.Open(ref path, ref miss, ref readOnly,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss);
//Get the tables in the word Document
foreach (Table tb in docs.Tables)
{
for (int row = 1; row <= tb.Rows.Count; row++)
{
for (int mycols = 1; mycols <= tb.Columns.Count; mycols++)
{
var cells = tb.Cell(row, mycols).Range.Paragraphs;
//Create a List using your custom formatter Class
List<Formatter> lFormatter = new List<Formatter>();
foreach (Paragraph para in cells)
{
Formatter formatter = new Formatter();
formatter.strCellText = para.Range.Text;
formatter.flIndent = para.LeftIndent;
formatter.strBullet = para.Range.ListFormat.ListString;
rTxBxResult.AppendText(formatter.strCombine);
lFormatter.Add(formatter);
}
for (int i =0; i< lFormatter.Count; i++)
{
Formatter formatter = lFormatter[i];
if(i==0)
excelworkSheet.Cells[row, mycols] = ((string)(excelworkSheet.Cells[row, mycols] as Excel.Range).Value2) + formatter.strCombine;
else
excelworkSheet.Cells[row, mycols] = ((string)(excelworkSheet.Cells[row, mycols] as Excel.Range).Value2) + Environment.NewLine + formatter.strCombine;
}
}
}
}
}
}
//Use this class to store the collected values from the rows and colums of the word table
public class Formatter
{
public string strCellText;
public string strIndent = "";
public float flIndent;
public string strBullet;
//Combine the pieces together and manipulate the strings as needed
public string strCombine
{
get
{
//first indent is 36 so 1 tab, second indent is 72 so 2 tabs, etc
//alternate * and dashes for bullet marks in excel using odd and even numbers
decimal newIndent = Math.Round((decimal)(flIndent / 36));
for (int i = 0; i < newIndent; i++)
{
strIndent = strIndent + " ";
}
if (newIndent == 0)
strBullet = "";
else if (IsOdd((int)newIndent))
strBullet="*";
else
strBullet = "-";
return strIndent + strBullet + strCellText;
}
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
}