如何将Datagridview导出到Excel排除不可见的行?

时间:2015-11-18 03:02:23

标签: visual-studio-2013 datagridview

帮助我们。下面的这个按钮代码允许我将过滤的行值导出到excel。

我添加了一个单选按钮,使某些行不可见 当我按下我的导出按钮(下面的代码)时,它也会导出我的不可见行。 我想添加一个if语句来检查row.visible = false是否从导出

跳过它

我是Coding的新手,所以请帮帮我

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim xlapp As Microsoft.Office.Interop.Excel.Application
    Dim xlworkbook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlworksheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misvalue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer

    xlapp = New Microsoft.Office.Interop.Excel.Application
    xlworkbook = xlapp.Workbooks.Add(misvalue)
    xlworksheet = xlworkbook.Sheets("sheet1")


    For i = 0 To DataGridView1.RowCount - 1
        For j = 0 To DataGridView1.ColumnCount - 1
            For k As Integer = 1 To DataGridView1.Columns.Count
                xlworksheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
                xlworksheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value
            Next
        Next
    Next

    xlworksheet.SaveAs("E:\vbexcel.xlsx")
    xlworkbook.Close()
    xlapp.Quit()

    releaseObject(xlapp)
    releaseObject(xlworkbook)
    releaseObject(xlworksheet)

    MsgBox("You can find the file E:\vbexcel.xlsx")
    Dim res As MsgBoxResult
    res = MsgBox("Process completed, Would you like to open the file?", MsgBoxStyle.YesNo)
    If (res = MsgBoxResult.Yes) Then
        Process.Start("E:\vbexcel.xlsx")
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

只需检查该行是否可见。如果它可见,则进程跳过它。

    <?php
require('fpdf.php');

class PDF extends FPDF
{
    // Page header
    function Header()
    {
        // Logo
        //$this->Image('logo.jpg',10,6,30);
        // Arial bold 15
        $this->SetFont('Arial','B',15);
        // Move to the right
        $this->Cell(80);
        // Title
        $this->Cell(60,10,'Convert HTML TO PDF',1,0,'C');
        // Line break
        $this->Ln(20);
    }

    // Page footer
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);

$pdf->Cell(0,10,'Name : ',0,1);
$pdf->Cell(0,10,'Email : ',0,1);
$pdf->Cell(0,10,'Mobile : ',0,1);
$pdf->Cell(0,10,'Comment : ',0,1);
$pdf->Output("filename.pdf","F");
$pdfdoc = $pdf->Output("", "S");
//$attachment = chunk_split(base64_encode($pdfdoc));

$email_to      = "xxx@gmail.com"; // The email you are sending to (example)
$email_from    = "xxx@outlook.com"; // The email you are sending from (example)
$email_subject = "subject line"; // The Subject of the email
$email_txt     = "text body of message"; // Message that the email has in it
$fileatt_type  = "application/pdf"; // File Type
$fileatt_name  = "filename.pdf"; // Filename that will be used for the file as the attachment
$semi_rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers       = "From: NameHere"; // Who the email is from (example)
$headers      .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
$email_message.= "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($pdfdoc));
$email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" . "--{$mime_boundary}--\n";
;

if(mail($email_to,$email_subject,$email_message,$headers))
{
    echo "File Sent Successfully.";
    //unlink($attachment); // delete a file after attachment sent.
}
else
{
    die("Sorry but the email could not be sent. Please go back and try again!");
    echo "File Not Sent .";
}
}

?>