C # how to set the format in Excel

Source: Internet
Author: User

① How to export data from the DataGrid to excel for printing?

② Previously, tablestyle has been set for the DataGrid, that is, the column title and the column to be displayed have been customized. What should I do if I want to export data in a custom view?

③ After exporting data to excel, how can I set a border for it?

④ How to align a column exported from DataGrid to excel in the center?

⑤ After the data is exported from the DataGrid to excel, how does one display the title row on each page during printing?

6. How can I achieve this when the data in the DataGrid is exported to excel and the current page/total pages are displayed on each page?

Private void button#click (Object sender, system. eventargs E)

{

Int row_index, col_index;

Row_index = 1;

Col_index = 1;

Excel. applicationclass Excel = new excel. applicationclass ();

Excel. workbooks. Add (true );

Datatable dt = Ds. Tables ["table"];

Foreach (datacolumn dcheader in DT. columns)

Excel. cells [row_index, col_index ++] = dcheader. columnname;

Foreach (datarow DR in DT. Rows)

{

Col_index = 0;

Foreach (datacolumn DC in DT. columns)

{

Excel. cells [row_index + 1, col_index + 1] = Dr [DC];

Col_index ++;

}

Row_index ++;

}

Excel. Visible = true;

}

Private void form1_load (Object sender, system. eventargs E)

{

Sqlconnection conn = new sqlconnection ("Server = Tao; uid = sa; Pwd =; database = pubs ");

Conn. open ();

Sqldataadapter da = new sqldataadapter ("select * from authors", Conn );

DS = new dataset ();

Da. Fill (DS, "table ");

Datagrid1.datasource = Ds;

Datagrid1.datamember = "table ";

}

② Datagrid1.tablestyles [0]. gridcolumnstyles [Index]. headertext; // The index can be from 0 ~ Datagrid1.tablestyles [0]. gridcolumnstyles. Count traversal.

③ Excel. Range range;

Range = worksheet. get_range (worksheet. cells [1, 1], XST. cells [Ds. tables [0]. rows. count + 1, DS. tables [0]. columns. count]);

Range. borderaround (Excel. xllinestyle. xlcontinuous, Excel. xlborderweight. xlthin, Excel. xlcolorindex. xlcolorindexautomatic, null );

Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. colorindex = excel. xlcolorindex. xlcolorindexautomatic;

Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. linestyle = excel. xllinestyle. xlcontinuous;

Range. Borders [Excel. xlbordersindex. xlinsidehorizontal]. Weight = excel. xlborderweight. xlthin;

Range. Borders [Excel. xlbordersindex. xlinsidevertical]. colorindex = excel. xlcolorindex. xlcolorindexautomatic;

Range. Borders [Excel. xlbordersindex. xlinsidevertical]. linestyle = excel. xllinestyle. xlcontinuous;

Range. Borders [Excel. xlbordersindex. xlinsidevertical]. Weight = excel. xlborderweight. xlthin;

④ Range. horizontalalignment = excel. xlhalign. xlhaligncenter

⑤ Worksheet. pagesetup. printtitlerows = "$1: $1 ";

⑥ Worksheet. pagesetup. centerfooter = "Page & P/total & N ";

22. When you assign the cell content of the DataGrid to excel, you want to display the progress on the DataGrid captiontext, but not the progress. Why?

...

Datagrid1.captiontext = "exporting:" + (row + 1) + "/" + row_cnt;

System. Windows. Forms. application. doevents ();

...

 

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.