. Net export word

Source: Internet
Author: User
Tags rowcount
Public static void exporttoword (datagridview dgv, progressbar progress, savefiledialog SaveFile) {Microsoft. office. interOP. word. document worddoc = new Microsoft. office. interOP. word. document (); Microsoft. office. interOP. word. table wordtable; object wordobj; If (dgv. rows. count = 0) {return;} else {SaveFile. addextension = true; SaveFile. defaultext = ". doc "; SaveFile. createprompt = true; SaveFile. title = "export file storage path"; SaveFile. filter = "Word files (*. doc) | *. doc "; if (SaveFile. showdialog () = dialogresult. OK) {progress. visible = true; Object Path = SaveFile. filename; wordobj = system. reflection. missing. value; // create a word object Microsoft. office. interOP. word. application word = new Microsoft. office. interOP. word. application (); worddoc = word. documents. add (ref wordobj, ref wordobj); // create a table // generate the Word Table file wordtable = worddoc. tables. add (worddoc. paragraphs. last. range, dgv. rowcount, dgv. columncount, ref wordobj, ref wordobj); wordtable. columns. setwidth (50, Microsoft. office. interOP. word. wdrulerstyle. wdadjustnone); try {for (INT I = 0; I <dgv. columns. count; I ++) // set the title {wordtable. cell (0, I + 1 ). range. TEXT = dgv. columns [I]. headertext; wordtable. cell (0, I + 1 ). range. font. size = 5 ;}for (INT I = 1; I <dgv. rows. count; I ++) // fill in the Data {for (Int J = 0; j <dgv. columns. count; j ++) {wordtable. cell (I + 1, J + 1 ). range. TEXT = dgv [J, I-1]. value. tostring (); wordtable. cell (I + 1, J + 1 ). range. font. size = 5;} progress. value ++ = 100/dgv. rowcount;} worddoc. saveas (ref path, ref wordobj, ref wordobj, ref wordobj, ref wordobj); worddoc. close (ref wordobj, ref wordobj, ref wordobj); Progress. value = 100; MessageBox. show ("data has been exported to:" + SaveFile. filename. tostring (), "exported", messageboxbuttons. OK, messageboxicon. information); Progress. value = 0; progress. visible = false;} catch (exception e) {MessageBox. show (E. message, "Reminder", messageboxbuttons. OK);} finally {word. quit (ref wordobj, ref wordobj, ref wordobj );}}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.