Bor*_*ski 6 c# excel ms-word openxml
有没有办法将Word文档转换为Excel文件?转换表格会非常有帮助.
像这样的东西:
我的意思是
void OpenWordDoc(string filePath)
{
_documentWord = SpreadsheetDocument.Open(filePath, true);
}
List<string> GetAllTablesXMLTags()
{
//find and copy
}
List<string> CreateExcelFile(string filePath)
{
TemplateExcelDocument excelDocument = new TemplateExcelDocument();
_documentExcel = excelDocument.CreatePackage(filePath);
}
void InsertXmlTagsToExcelFile(string filePath)
{
CreateExcelFiles(filePath);
var xmlTable = GetAllTablesXMLTags();
// ... insert to _documentExcel
}
Run Code Online (Sandbox Code Playgroud)
要获取 docx 文件中的所有表格,您可以使用下面的代码:
using System;
using Independentsoft.Office;
using Independentsoft.Office.Word;
using Independentsoft.Office.Word.Tables;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
WordDocument doc = new WordDocument("c:\\test.docx");
Table[] tables = doc.GetTables();
foreach (Table table in tables)
{
//read data
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要将它们写入 Excel 文件,您必须对每个单元格执行以下操作:
app.Visible = false;
workbooks = app.Workbooks;
workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
sheets = workbook.Worksheets;
worksheet = (_Worksheet)sheets.get_Item(1);
excel(row, column, "value");
workbook.Saved = true;
workbook.SaveAs(output_file);
app.UserControl = false;
app.Quit();
Run Code Online (Sandbox Code Playgroud)
最后excel函数如下:
public void excel(int row, int column, string value)
{
worksheet.Cells[row, column] = value;
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用CSV或HTMLformat 来创建 Excel 文件。为此,只需example.xlsx为 CSV 逗号分隔创建一个包含以下内容的文件:
列 1,列 2,列 3,列 4 \n
val1,val2,val3val4 \n
或 HTML 格式:
<table>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>val1</td>
<td>val2</td>
<td>val3</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3450 次 |
| 最近记录: |