C# 使用iTextSharp和WPF DataGrid导出PDF的代码中是否有错误?
我一直在尝试使用从Nuget下载的iTextSharp将我的DataGrid数据导出到PDF,但是当我单击ExportToPdf按钮时,什么都没有发生,看起来像是空的单击事件,也没有发生编译错误。我在这里尝试了许多解决方案,但仍然不起作用。 Hare是我的Xaml:C# 使用iTextSharp和WPF DataGrid导出PDF的代码中是否有错误?,c#,wpf,datagrid,itext,C#,Wpf,Datagrid,Itext,我一直在尝试使用从Nuget下载的iTextSharp将我的DataGrid数据导出到PDF,但是当我单击ExportToPdf按钮时,什么都没有发生,看起来像是空的单击事件,也没有发生编译错误。我在这里尝试了许多解决方案,但仍然不起作用。 Hare是我的Xaml: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AMD.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Customers Page" Loaded="Page_Loaded">
<!--<Page.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}" Value="">
<Setter Property="b" Value="" />
</DataTrigger>
</Style.Triggers>
</Style>
</Page.Resources>-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button x:Name="GeneratePDF" Grid.Column="1" HorizontalAlignment="Right"
Style="{StaticResource MaterialDesignRaisedDarkButton}"
Width="120" ToolTip="Generate PDF" Margin="10 10 20 10"
Click="GeneratePDF_Click">
Export As PDF
</Button>
</Grid>
<Grid Grid.Row="1">
<DataGrid x:Name="dgvCustomers" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=CustomerName, TargetNullValue=------}" Header="Customer Name" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=DateBought, TargetNullValue=------}" Header="Date Bought" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=ItemName, TargetNullValue=------}" Header="Item Name" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Price, TargetNullValue=------}" Header="Price" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Quantity, TargetNullValue=------}" Header="Quantity" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=SubTotal, TargetNullValue=------}" Header="Sub Total" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Total, TargetNullValue=------}" Header="Total" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=ReducedPrice, TargetNullValue=------}" Header="Discount" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=PaymentType, TargetNullValue=------}" Header="Payment Type" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Cash, TargetNullValue=------}" Header="Cash" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Transfer, TargetNullValue=------}" Header="Transfer" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Bank, TargetNullValue=------}" Header="Bank" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Comment, TargetNullValue=------}" Header="Comment" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
</Page>
和C:
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace AMD.Views
{
/// <summary>
/// Interaction logic for CustomersPage.xaml
/// </summary>
public partial class CustomersPage : Page
{
public CustomersPage()
{
InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var db = new AMDDataContext();
var query = from p in db.Customers
select p;
dgvCustomers.ItemsSource = query;
var query1 = from p in db.Customers
where p.DateBought.Value != null
select p;
//CustomersByDate.ItemsSource = query1.ToList();
//CustomersByDate.DisplayMemberPath = "DateBought";
//CustomersByDate.SelectedValuePath = "Id";
CustomersSelectionType.Items.Add("Daily");
CustomersSelectionType.Items.Add("Weekly");
CustomersSelectionType.Items.Add("Monthly");
CustomersSelectionType.Items.Add("Yearly");
}
private void GeneratePDF_Click(object sender, RoutedEventArgs e)
{
PdfPTable table = new PdfPTable(dgvCustomers.Columns.Count);
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, new System.IO.FileStream("Test.pdf",
System.IO.FileMode.Create));
doc.Open();
for (int j = 0; j < dgvCustomers.Columns.Count; j++)
{
table.AddCell(new Phrase(dgvCustomers.Columns[j].Header.ToString()));
}
table.HeaderRows = 1;
IEnumerable itemsSource = dgvCustomers.ItemsSource as IEnumerable;
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
DataGridRow row = dgvCustomers.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (row != null)
{
DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);
for (int i = 0; i < dgvCustomers.Columns.Count; i++)
{
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock txt = cell.Content as TextBlock;
if (txt !=null)
{
table.AddCell(new Phrase(txt.Text));
}
}
}
}
doc.Add(table);
doc.Close();
}
}
private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
}
代码中没有错误,但它什么也不做。提前谢谢你 是的!正如@那个家伙所说的,它是有效的。在我的计算机上进行了一些搜索之后,我发现它保存在我的项目的调试文件夹中。谢谢你@那个家伙。有些地方可以改进,但快速测试表明它是有效的。生成包含数据网格内容的PDF。尝试设置文件流的绝对路径,如C:\Any\path\Test.pdf。可能文件不是在您认为的位置生成的。如何打开对话框来选择保存文件的位置?请帮忙。为什么不用硬编码的路径来测试呢?对话增加了不必要的复杂性和进一步的潜在问题,而不是帮助解决问题。好的,我理解。非常感谢。