C # call Excel save code below

Source: Internet
Author: User
Tags count tostring
Excel

private void Btnsaveasdata_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 measured results table-Save as";

DataTable myDT1, myDT2;

MyDT1 = DataTable (A,A1);
MyDT2 = DataTable (A,A1);
Double Mingaocheng = 0;//get_min (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;
int Zhanyonghang = 3;
Xlssheet.cells[2, 7] = lbldmmc.text + "section measured results table";
xlssheet.cells[zhanyonghang+2, Intcols + 1] = "SYSTEM";
Xlssheet.cells[zhanyonghang + 3, Intcols + 1] = "TABLE";
Xlssheet.cells[zhanyonghang + 4, Intcols + 1] = ":";
Xlssheet.cells[zhanyonghang + 7, Intcols + 1] = "one";
Xlssheet.cells[zhanyonghang + 8, Intcols + 1] = "school";
Xlssheet.cells[zhanyonghang + 9, Intcols + 1] = ":";
Xlssheet.cells[zhanyonghang +, Intcols + 1] = "two";
Xlssheet.cells[zhanyonghang +, Intcols + 1] = "school";
Xlssheet.cells[zhanyonghang +, Intcols + 1] = ":";
Xlssheet.cells[zhanyonghang +, Intcols + 1] = "inspection";
Xlssheet.cells[zhanyonghang +, Intcols + 1] = "collect";
Xlssheet.cells[zhanyonghang +, Intcols + 1] = ":";
Xlssheet.cells[zhanyonghang + 1, 3] = "Date measured:" + dtps3.text;
Xlssheet.cells[zhanyonghang + 1, 7] = "spud tool: Spud Rod";
Xlssheet.cells[zhanyonghang + 1, 10] = "Water level:" + Txts4.text + "M";
Xlssheet.cells[1, Intcols] = "tabulation:";
for (int j = 0; J < Intcols; J + +)
{
Xlssheet.cells[zhanyonghang + 2, j + 1] = Strcolname[j];
}
int tempi = 1;

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)
         & nbsp;          {
                         mydt1.rows[i][ J] = Mydt1.rows[i][j]. ToString () + "";
                    }
                     Xlsapp.cells[zhanyonghang + i + 3, j + 1] = Mydt1.rows[i][j];
               }
           }

Xlssheet.cells[zhanyonghang + introws + 3,6] = "NOTE:" + txtfuzhu.text;
XlsSheet.Columns.ColumnWidth = 9;
Xlssheet.get_range (Xlssheet.cells[zhanyonghang + 2, Intcols + 1], Xlssheet.cells[zhanyonghang + intRows + 2, IntCols + 1]) . ColumnWidth = 3;

XlsSheet.Rows.RowHeight = 18;
Xlssheet.get_range (Xlssheet.cells[zhanyonghang + 2, 1], Xlssheet.cells[zhanyonghang + 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.Size =;

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

Xlssheet.get_range (Xlssheet.cells[zhanyonghang + temprows3 + 3, TEMPCOLS3), Xlssheet.cells[zhanyonghang + 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[zhanyonghang + i + 3, j + 1] = 0;
}
else if (Temp1 = 0.123456)
{
Xlssheet.cells[i + 3, j + 1] = "";
}
Else
{
Xlssheet.get_range (Xlssheet.cells[zhanyonghang + i + 3, j + 1], Xlssheet.cells[zhanyonghang + i + 3, J + 1]). numberformatlocal = "#####";
}
}
Else
{
if (Temp1 > 9)
{
Xlssheet.get_range (Xlssheet.cells[zhanyonghang + i + 3, j + 1], Xlssheet.cells[zhanyonghang + i + 3, J + 1]). numberformatlocal = "#####.0";
}
}
}
}
Xlssheet.get_range (xlssheet.cells[3, 6], xlssheet.cells[3, 6]). numberformatlocal = "#####.0";
Xlssheet.get_range (Xlssheet.cells[3, 4], Xlssheet.cells[introws + 2, 4]). numberformatlocal = "#####.0";
Xlssheet.get_range (xlssheet.cells[3, 6], Xlssheet.cells[introws + 2, 6]). numberformatlocal = "#####.0";
Xlssheet.get_range (Xlssheet.cells[3, 8], Xlssheet.cells[introws + 2, 8]). numberformatlocal = "#####.0";
Xlssheet.get_range (Xlssheet.cells[3, ten], Xlssheet.cells[introws + 2, 10]). numberformatlocal = "#####.0";
Xlssheet.get_range (Xlssheet.cells[3,), Xlssheet.cells[introws + 2, 12]). numberformatlocal = "#####.0";

XlsSheet.Columns.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
XlsSheet.Columns.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
Xlssheet.get_range (Xlssheet.cells[zhanyonghang + 1, Intcols], Xlssheet.cells[zhanyonghang + 1, IntCols]). HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;

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

            String FileName = Xlsapp.getsaveasfilename (" Section measured results table. xls "," Excel file (*.xls), *.xls ", 1," section test results Table "," section measured Results table "). ToString (). Trim ();
            if (filename!= "False" && filename!= "")
            {
                 Xlssheet.saveas (FileName, Excel.XlFileFormat.xlExcel9795, "", "", False, False, False, 1, 1, true;
           }
            xlsbook.saved = true;
            xlsApp.Workbooks.Close ();
            xlsapp.quit ();
       } 



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.