public static void Print_DataGridView(DataGridView dgv1)
{
try
{
try
{
string fileName;
ExcelWriter writer;
dgv = dgv1;
AvailableColumns.Clear();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
AvailableColumns.Add(column.HeaderText);
}
}
PrintOptions options = new PrintOptions(AvailableColumns);
if (options.ShowDialog() == DialogResult.OK)
{
PrintTitle = options.PrintTitle;
PrintAllRows = options.PrintAllRows;
FitToPageWidth = options.FitToPageWidth;
SelectedColumns = options.GetSelectedColumns();
if (!options.EX_excel)
{
goto Label_0397;
}
fileName = @"c:\test.xls";
SaveFileDialog dialog2 = new SaveFileDialog
{
Filter = "Excel文件(*.xls)|*.xls",
Title = "保存Excel文档"
};
if (dialog2.ShowDialog() == DialogResult.OK)
{
fileName = dialog2.FileName;
goto Label_012C;
}
}
return;
Label_012C:
writer = new ExcelWriter(fileName);
writer.BeginWrite();
int num = 0;
foreach (DataGridViewColumn column2 in dgv.Columns)
{
if (column2.Visible && SelectedColumns.Contains(column2.HeaderText))
{
short y = short.Parse(num.ToString());
writer.WriteString(0, y, column2.HeaderText);
num++;
}
}
for (int i = 0; i < dgv.Rows.Count; i++)
{
if (!dgv.Rows[i].IsNewRow && (PrintAllRows || dgv.Rows[i].Selected))
{
short x = short.Parse((i + 1).ToString());
int num6 = 0;
foreach (DataGridViewColumn column2 in dgv.Columns)
{
if (column2.Visible && SelectedColumns.Contains(column2.HeaderText))
{
short num7 = short.Parse(num6.ToString());
string str2 = "";
if (dgv.Rows[i].Cells[column2.Name].Value != null)
{
str2 = dgv.Rows[i].Cells[column2.Name].Value.ToString().Trim();
}
writer.WriteString(x, num7, str2);
num6++;
}
}
}
}
writer.EndWrite();
MessageBox.Show("保存成功");
return;
Label_0397:
if (options.Print_isname && (options.Print_name != ""))
{
printDoc.PrinterSettings.PrinterName = options.Print_name;
}
RowsPerPage = 0;
PrintPreviewDialog dialog = new PrintPreviewDialog
{
Document = printDoc
};
printDoc.BeginPrint += new PrintEventHandler(PrintDGV.PrintDoc_BeginPrint);
printDoc.PrintPage += new PrintPageEventHandler(PrintDGV.PrintDoc_PrintPage);
if (dialog.ShowDialog() != DialogResult.OK)
{
printDoc.BeginPrint -= new PrintEventHandler(PrintDGV.PrintDoc_BeginPrint);
printDoc.PrintPage -= new PrintPageEventHandler(PrintDGV.PrintDoc_PrintPage);
}
else
{
printDoc.Print();
printDoc.BeginPrint -= new PrintEventHandler(PrintDGV.PrintDoc_BeginPrint);
printDoc.PrintPage -= new PrintPageEventHandler(PrintDGV.PrintDoc_PrintPage);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "错误!!!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
finally
{
}
}
private static void PrintDoc_BeginPrint(object sender, PrintEventArgs e)
{
try
{
StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Near;
StrFormat.LineAlignment = StringAlignment.Center;
StrFormat.Trimming = StringTrimming.EllipsisCharacter;
StrFormatComboBox = new StringFormat();
StrFormatComboBox.LineAlignment = StringAlignment.Center;
StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap;
StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter;
ColumnLefts.Clear();
ColumnWidths.Clear();
ColumnTypes.Clear();
CellHeight = 0;
RowsPerPage = 0;
CellButton = new Button();
CellCheckBox = new CheckBox();
CellComboBox = new ComboBox();
TotalWidth = 0;
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible && SelectedColumns.Contains(column.HeaderText))
{
TotalWidth += column.Width;
}
}
PageNo = 1;
NewPage = true;
RowPos = 0;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}