① 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 ();
...