C # Implementation inserts rows and columns into Excel, and sets the cell Merge center effect

Source: Internet
Author: User

Insert Blank line:

Microsoft.Office.Interop.Excel.Workbook Xlsworkbook;

Microsoft.Office.Interop.Excel.Worksheet xlssheet = xlsworkbook.worksheets[1];

Microsoft.Office.Interop.Excel.Range xlsrow= (Microsoft.Office.Interop.Excel.Range) xlssheet.rows[3,misvalue];

Xlsrow.insert (Microsoft.office.interop.excel.xlshiftdown,misvalue);

Insert an empty column:

Excel.Range xlscolumns = (excel.range) ws. Columns[index, Type.Missing];

Xlscolumns.insert (Excel.XlInsertShiftDirection.xlShiftToRight, Type.Missing);

String columnletter = Getletter (index-1);
Ws. cells[2, index] = TxtBoxExpenseType.Text.Trim ();
Excel.Range Newexpensetyperange = Ws.get_range (string. Format ("{0}{1}", Columnletter, 2), String. Format ("{0}{1}", Columnletter, 3));
Newexpensetyperange.mergecells = true;
Newexpensetyperange.verticalalignment = Excel.XlVAlign.xlVAlignCenter;
Newexpensetyperange.horizontalalignment = Excel.XlVAlign.xlVAlignCenter;

public static string getletter (int index)
             if (Index < 0) {throw new Exception ("Invalid parameter "); }

List chars = new list ();
if (chars. Count > 0) index--;
Chars. Insert (0, ((char) (index% + (int) ' A ')). ToString ());
index = (int) ((index-index% 26)/26);
} while (index > 0);

Return String.Join (String. Empty, chars. ToArray ());

C # Implementation inserts rows and columns into Excel, and sets the cell Merge center effect

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.