aspose.words DataTable 如何转成Table
时间: 2023-12-06 15:45:23 浏览: 90
你可以使用Aspose.Words提供的Table类将DataTable转换为表格。下面是一个示例代码:
```csharp
// 创建一个新的表格
Table table = new Table(doc);
// 获取要转换的数据表
DataTable dataTable = GetDataTable();
// 添加表格标题
Row headerRow = new Row(doc);
headerRow.RowFormat.HeadingFormat = true;
headerRow.RowFormat.Alignment = RowAlignment.Center;
Cell headerCell = new Cell(doc);
headerCell.CellFormat.HorizontalMerge = CellMerge.First;
headerCell.CellFormat.VerticalMerge = CellMerge.First;
headerCell.CellFormat.Width = 50;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.Borders.ClearFormatting();
headerCell.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single;
headerCell.CellFormat.Borders.Bottom.Color = Color.Black;
headerCell.CellFormat.Borders.Left.LineStyle = LineStyle.Single;
headerCell.CellFormat.Borders.Left.Color = Color.Black;
headerCell.CellFormat.Borders.Right.LineStyle = LineStyle.Single;
headerCell.CellFormat.Borders.Right.Color = Color.Black;
headerCell.CellFormat.Borders.Top.LineStyle = LineStyle.Single;
headerCell.CellFormat.Borders.Top.Color = Color.Black;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.VerticalMerge = CellMerge.None;
headerCell.CellFormat.HorizontalMerge = CellMerge.None;
headerCell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
headerCell.CellFormat.Borders.LineStyle = LineStyle.Single;
headerCell.CellFormat.Borders.Color = Color.Black;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerCell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
headerCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
headerCell.CellFormat.Alignment = CellAlignment.Center;
headerCell.CellFormat.FitText = true;
headerCell.CellFormat.WrapText = true;
headerRow.AppendChild(headerCell);
table.AppendChild(headerRow);
// 添加表格内容
foreach (DataRow dataRow in dataTable.Rows)
{
Row row = new Row(doc);
foreach (DataColumn dataColumn in dataTable.Columns)
{
Cell cell = new Cell(doc);
cell.CellFormat.HorizontalMerge = CellMerge.None;
cell.CellFormat.VerticalMerge = CellMerge.None;
cell.CellFormat.Width = 50;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.Borders.ClearFormatting();
cell.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single;
cell.CellFormat.Borders.Bottom.Color = Color.Black;
cell.CellFormat.Borders.Left.LineStyle = LineStyle.Single;
cell.CellFormat.Borders.Left.Color = Color.Black;
cell.CellFormat.Borders.Right.LineStyle = LineStyle.Single;
cell.CellFormat.Borders.Right.Color = Color.Black;
cell.CellFormat.Borders.Top.LineStyle = LineStyle.Single;
cell.CellFormat.Borders.Top.Color = Color.Black;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat.WrapText = true;
cell.CellFormat.Shading.BackgroundPatternColor = Color.White;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.CellFormat.Alignment = CellAlignment.Center;
cell.CellFormat.FitText = true;
cell.CellFormat
阅读全文