C # call Excel print code below

Source: Internet
Author: User
Tags count tostring
excel| Printing

private void Btnprintdata_click (object sender, EventArgs e)
{
Excel.Application Xlsapp = new Excel.Application ();
Excel._workbook Xlsbook = XLSAPP.WORKBOOKS.ADD (Excel.XlWBATemplate.xlWBATWorksheet);
Excel._worksheet Xlssheet = (excel._worksheet) xlsbook.worksheets[1];
Xlssheet.activate ();

Xlsapp.caption = lbldmmc.text + "section test results Table-Print Preview";
DataTable MYDT1,MYDT2;

MyDT1 = DataTable (A,A1);
MyDT2 = DataTable (A,A1);

string[] Strcolname = new string[] {"Starting from M", "River bottom elevation M", "starting from M", "River bottom elevation M", "starting from M", "River bottom elevation M", "starting from M", "River bottom elevation M", "starting from M", "River bottom elevation M", "Starting from M", "River bottom elevation M", ""};
int intcols = strcolname.length-1;
int introws = MyDT1.Rows.Count;

int[] intRows1 = new Int[mydt1.rows.count];
int[] intcols1 = new Int[mydt1.columns.count];

int minrow=0;
int mincol=0;
Xlssheet.cells[2, Intcols + 1] = "SYSTEM";
Xlssheet.cells[3, Intcols + 1] = "TABLE";
Xlssheet.cells[4, Intcols + 1] = ":";
Xlssheet.cells[7, Intcols + 1] = "one";
Xlssheet.cells[8, Intcols + 1] = "school";
Xlssheet.cells[9, Intcols + 1] = ":";
XLSSHEET.CELLS[12, Intcols + 1] = "two";
xlssheet.cells[13, Intcols + 1] = "school";
XLSSHEET.CELLS[14, Intcols + 1] = ":";
XLSSHEET.CELLS[17, Intcols + 1] = "inspection";
xlssheet.cells[18, Intcols + 1] = "collect";
xlssheet.cells[19, Intcols + 1] = ":";
Xlssheet.cells[1, 3] = "Date measured:" + dtps3.text;
Xlssheet.cells[1, 7] = "Spud tool:" + txtctgongju.text;
Xlssheet.cells[1, 10] = "Water level:" + Txts4.text + "M";
Xlssheet.cells[1, Intcols] = "tabulation:";


for (int j = 0; J < Intcols; J + +)
{
Xlssheet.cells[2, j + 1] = Strcolname[j];
}

for (int i = 0; i < introws; i++)
{
for (int j = 0; J < Intcols; J + +)
{
if (i = = Temprows2 && j = = TEMPCOLS2)
{
MYDT1.ROWS[I][J]=MYDT1.ROWS[I][J]. ToString () + "▲";
}

if (i = = Temprows1 && j = = TEMPCOLS1)
{
MYDT1.ROWS[I][J] = Mydt1.rows[i][j]. ToString () + "";
}

MYDT1.ROWS[TEMPROWS3 + 3][TEMPCOLS3] = mydt1.rows[temprows3 + 3][TEMPCOLS3] + "";
MYDT1.ROWS[TEMPROWS3 + 3][TEMPCOLS3 + 1] = mydt1.rows[temprows3 + 3][TEMPCOLS3 + 1] + "";

                 Xlsapp.cells[i + 3, j + 1] = mydt1.rows[i][j];                   
                }
           }
            Xlssheet.cells[introws + 3, 9] = "NOTE:" + Txtfuzhu.text;
            xlsSheet.Columns.ColumnWidth = 9;
            Xlssheet.get_range (xlssheet.cells[2, Intcols + 1], Xlssheet.cells[introws + 2, Intcols + 1]). ColumnWidth = 3;

XlsSheet.Rows.RowHeight = 18;
Xlssheet.get_range (xlssheet.cells[2, 1], xlssheet.cells[2, Intcols]). RowHeight = 15;

XlsSheet.Cells.Font.Name = "Song Body";
XlsSheet.Cells.Font.Size = 10;
XlsSheet.Cells.Font.Bold = false;
XlsSheet.Cells.WrapText = false;
Xlssheet.get_range (xlssheet.cells[2, 1], xlssheet.cells[2, Intcols]). Font.Name = "Song Body";
Xlssheet.get_range (xlssheet.cells[2, 1], xlssheet.cells[2, Intcols]). Font.Size = 10;
Xlssheet.get_range (xlssheet.cells[2, 1], xlssheet.cells[2, Intcols]). Font.Bold = true;
Xlssheet.get_range (xlssheet.cells[2, 1], xlssheet.cells[2, Intcols]). WrapText = false;

Xlssheet.get_range (XLSSHEET.CELLS[TEMPROWS3 + 3, TEMPCOLS3], XLSSHEET.CELLS[TEMPROWS3 + 3, tempcols3+1]). Font.underline = true;


for (int i = 0; i < MyDT2.Rows.Count; i++)
{
for (int j = 0; J < MyDT2.Columns.Count; J + +)
{
Double Temp1;
if (mydt2.rows[i][j). ToString () = "")
{
Temp1 = 0.123456;
}
Else
{
Temp1 = Convert.todouble (Mydt2.rows[i][j]. ToString ());
}

if (j% 2 = 0)
{
if (Temp1 = 0)
{
Xlssheet.cells[i + 3, j + 1] = 0;
}
else if (Temp1 = 0.123456)
{
Xlssheet.cells[i + 3, j + 1] = "";
}
Else
{
Xlssheet.get_range (Xlssheet.cells[i + 3, j + 1], Xlssheet.cells[i + 3, j + 1]). numberformatlocal = "#####";
}
}
Else
{
if (Temp1 > 9)
{
Xlssheet.get_range (Xlssheet.cells[i + 3, j + 1], Xlssheet.cells[i + 3, j + 1]). numberformatlocal = "#####.0";
}
}
}
}
XlsSheet.Columns.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
XlsSheet.Columns.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
Xlssheet.get_range (Xlssheet.cells[1, Intcols], xlssheet.cells[1, Intcols]). HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;

Xlssheet.get_range (xlssheet.cells[2, 1], Xlssheet.cells[introws + 2, Intcols]). Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
Xlssheet.get_range (xlssheet.cells[2, 1], Xlssheet.cells[introws + 2, Intcols]). Borders.Weight = Excel.XlBorderWeight.xlThin;

DGV. columns["Elevation 6"]. Defaultcellstyle.format = "### #0.0";
XlsSheet.PageSetup.CenterHeader = "&\" black body \ "&18" + lbldmmc.text + "section measured results table";
XlsSheet.PageSetup.CenterFooter = "&9-----&p page-----";

XlsSheet.PageSetup.PrintGridlines = false;
Xlssheet.get_range (Xlssheet.cells[1,1],xlssheet.cells[1,intcols]). Borders.LineStyle
XlsSheet.PageSetup.LeftMargin = xlsapp.inchestopoints (0.5);
XlsSheet.PageSetup.RightMargin = xlsapp.inchestopoints (0.5);
XlsSheet.PageSetup.TopMargin = xlsapp.inchestopoints (1.3);
XlsSheet.PageSetup.BottomMargin = xlsapp.inchestopoints (1.0);
XlsSheet.PageSetup.HeaderMargin = xlsapp.inchestopoints (0.9);
XlsSheet.PageSetup.FooterMargin = xlsapp.inchestopoints (0.6);
XlsSheet.PageSetup.CenterHorizontally = true;
XlsSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4;
XlsSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
Excel.Range GetRange = Xlssheet.get_range (xlssheet.cells[1, 1], xlssheet.cells[2, Intcols]);
XlsSheet.PageSetup.PrintTitleRows = Getrange.get_address (Getrange.row, Getrange.column, Excel.XlReferenceStyle.xlA1, 1, 1);


Xlsapp.visible = true;
Xlssheet.printpreview (TRUE);
Xlsbook.saved = true;
XlsApp.Workbooks.Close ();
Xlsapp.visible = false;
Xlsapp.quit ();
}




Related Article

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.