Asp.net export Excel settings format

Source: Internet
Author: User

From: http://hi.baidu.com/%B0%D9%C9% AB %BB%A8%CC%EF/blog/item/077a83b184ce03acd8335a47.html

Add reference: Microsoft Excel 11.0 Object Library;

Add: using Microsoft. Office. Interop. Excel;

1. Open an Excel file ==============================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. open (@ "E: \ aaa.xls", Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing );

Excel1.Visible = true;

2. Create an Excel file ==============================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (XlWBATemplate. xlWBATWorksheet or true );

Worksheet1.Activate (); // activate the table sheet1

Excel1.Visible = true;

3. Create an Excel table ============================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets. Add (Type. Missing, workbook1.Worksheets [1], 1, Type. Missing );

Excel1.Visible = true;

4. Rename an Excel table

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1" or 1];

Worksheet1.Name = "Work Schedule ";

Excel1.Visible = true;

5. set or modify the cell content of an Excel table ====================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Worksheet1.Cells [1, 1] = "name ";

Worksheet1.Cells [1, 2] = "gender ";

Excel1.Visible = true;

6. Set the row width and column height of the Excel table ==========================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Worksheet1.Columns. ColumnWidth = 20; // global row width

Worksheet1.Columns. RowHeight = 20; // global column height

Range range1 = (Range) worksheet1.Cells [2, 1];

Range1.Columns. ColumnWidth = 40; // cell row width

Range1.Columns. RowHeight = 40; // cell column height

Excel1.Visible = true;

7. Set the cell border of the Excel table ==========================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Range range1 = (Range) worksheet1.Cells [2, 2];

Range1.Borders. Color = System. Drawing. ColorTranslator. ToOle (Color. Red );

Range1.Borders. get_Item (XlBordersIndex. xlEdgeTop). LineStyle = XlLineStyle. xlContinuous;

Range1.Borders. get_Item (XlBordersIndex. xlEdgeRight). LineStyle = XlLineStyle. xlContinuous;

Range1.Borders. get_Item (XlBordersIndex. xlEdgeBottom). LineStyle = XlLineStyle. xlContinuous;

Range1.Borders. get_Item (XlBordersIndex. xlEdgeLeft). LineStyle = XlLineStyle. xlContinuous;

// You can also use the following code to replace range1.BorderAround (XlLineStyle. xlContinuous, XlBorderWeight. xlThin, XlColorIndex. xlColorIndexAutomatic, null );

Range1.Borders. get_Item (XlBordersIndex. xlDiagonalDown). LineStyle = XlLineStyle. xlContinuous; // slash

Range1.Borders. get_Item (XlBordersIndex. xlDiagonalUp). LineStyle = XlLineStyle. xlContinuous; // backslash

Range1.Borders. get_Item (XlBordersIndex. xlDiagonalDown). Color = System. Drawing. ColorTranslator. ToOle (Color. Gold );

Excel1.Visible = true;

8. Excel table block operations ============================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Range range1 = worksheet1.get _ Range ("A2", "E8"); // select the action block

Range1.Font. Bold = true; // set the blacklist

Range1.Font. Size = 18; // set the font Size

Range1.Font. Name = ""; // set the font

Range1.Font. Color = System. Drawing. ColorTranslator. ToOle (Color. Blue); // set the font Color

Range1.HorizontalAlignment = XlHAlign. xlHAlignCenter; // sets the horizontal aligncenter.

Range1.VerticalAlignment = XlVAlign. xlVAlignCenter; // you can specify the vertical aligncenter.

Range1.value2= "123 \ r \ n456 ";

Range1.Borders. get_Item (XlBordersIndex. xlEdgeTop). LineStyle = XlLineStyle. xlContinuous;

Range1.Borders. get_Item (XlBordersIndex. xlEdgeRight). LineStyle = XlLineStyle. xlContinuous;

Range1.Borders. get_Item (XlBordersIndex. xlEdgeBottom). LineStyle = XlLineStyle. xlContinuous;

Range1.Borders. get_Item (XlBordersIndex. xlEdgeLeft). LineStyle = XlLineStyle. xlContinuous;

// You can also use the following code to replace range1.BorderAround (XlLineStyle. xlContinuous, XlBorderWeight. xlThin, XlColorIndex. xlColorIndexAutomatic, null );

Range1.Borders. get_Item (XlBordersIndex. xlInsideHorizontal). LineStyle = XlLineStyle. xlContinuous; // block vertical line

Range1.Borders. get_Item (XlBordersIndex. xlInsideVertical). LineStyle = XlLineStyle. xlContinuous; // In-block horizontal line

Excel1.Visible = true;

9. Merge cells in an Excel table ============================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Range range1 = worksheet1.get _ Range ("A2", "E8"); // select the action block

Range1.value2= "123 \ r \ n456 ";

Excel1.Application. DisplayAlerts = false; // no warning message is displayed for the merge operation.

Range1.Merge (false); // If the parameter is True, each row is merged into a cell.

Excel1.Application. DisplayAlerts = true;

Excel1.Visible = true;

10. Copy an Excel table ==============================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Worksheet1.Cells [1, 1] = "123 ";

Worksheet1.Copy (Type. Missing, worksheet1 );

Worksheet worksheet2 = (Worksheet) worksheet1.Next;

// Worksheet2.Name = "Sheet2 ";

Excel1.Visible = true;

11. Page settings ==============================

Microsoft. Office. Interop. Excel. Application excel1 = new Microsoft. Office. Interop. Excel. Application ();

Workbook workbook1 = excel1.Workbooks. Add (true );

Excel1.Caption = "My reports ";

Worksheet worksheet1 = (Worksheet) workbook1.Worksheets ["sheet1"];

Worksheet1.PageSetup. PaperSize = XlPaperSize. xlPaperA3; // paper size

Worksheet1.PageSetup. PrintTitleRows = "$1: $3"; // top header line

Worksheet1.PageSetup. Orientation = XlPageOrientation. xlLandscape; // The page direction is horizontal.

Worksheet1.PageSetup. TopMargin = excel1.CentimetersToPoints (2); // The top margin is 2 cm (centimeter to pixel)

Worksheet1.PageSetup. BottomMargin = excel1.CentimetersToPoints (2); // The bottom margin is 2 cm (centimeter to pixel)

Worksheet1.PageSetup. LeftMargin = excel1.CentimetersToPoints (1.5); // The left margin is 1.5 cm (centimeter to pixel)

Worksheet1.PageSetup. RightMargin = excel1.CentimetersToPoints (1.5); // The right margin is 1.5 cm (centimeter to pixel)

Worksheet1.PageSetup. HeaderMargin = excel1.CentimetersToPoints (1.2); // the header margin is 1.2 cm (centimeter to pixel)

Worksheet1.PageSetup. FooterMargin = excel1.CentimetersToPoints (1); // The footer margin is 1 cm (centimeter to pixel)

Worksheet1.PageSetup. CenterHorizontally = true; // The page is horizontally centered.

Worksheet1.PageSetup. CenterVertically = false; // The page is not vertically centered.

Worksheet1.PageSetup. CenterFooter = "Page & P, total & N"; // content of the middle footer

Excel1.Visible = true;

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.