c# asp.net Excel

2024-11-29 06:45:28
推荐回答(1个)
回答(1):

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;

using System.Data;
using System.IO;
using System.Text;

using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
//using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

Object missing = Missing.Value;

Microsoft.Office.Interop.Excel.Application m_objExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks m_objWorkBooks = m_objExcel.Workbooks;

Microsoft.Office.Interop.Excel.Workbook m_objWorkBook = m_objWorkBooks.Add(true);
Microsoft.Office.Interop.Excel.Sheets m_objWorkSheets = m_objWorkBook.Sheets; ;
Microsoft.Office.Interop.Excel.Worksheet m_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objWorkSheets[1];
m_objExcel.Visible = true;

// Range range1 = m_objWorkSheet.get_Range("A2", "E8");//选择操作块
m_objExcel.Caption = "收支平衡报表";
//try
//{
// m_objExcel.Save("e:129.xls");
//}
//catch (Exception e)
//{

//}
//finally
//{
// m_objWorkBooks.Close();
// m_objExcel.Quit();
//}

// m_objExcel.Cells[1, 1] = "建筑节能评估计算报告书";//第一行第一列文赋值

// m_objExcel.Cells[1, 1] = "建筑节能评估计算报告书";
m_objExcel.get_Range(m_objExcel.Cells[1, 1], m_objExcel.Cells[1, 4]).Font.Size = 20;
Microsoft.Office.Interop.Excel.Range range1 = m_objExcel.get_Range(m_objExcel.Cells[1, 1], m_objExcel.Cells[4,7]);
// range1.Merge(Type.Missing);//合并单元格

m_objExcel.get_Range(m_objExcel.Cells[1, 2], m_objExcel.Cells[1, 4]).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue );//设置字体颜色
m_objExcel.get_Range(m_objExcel.Cells[1, 2], m_objExcel.Cells[1, 2]).Value2 = "2010年西安市财政局债务收支平衡表";//给合并的单元格赋值

m_objExcel.Cells.Columns.HorizontalAlignment = XlHAlign.xlHAlignCenter; //设置文本水平居中 成功
// m_objExcel.Cells[4, 5] = "单位:万元";
range1.Merge(Type.Missing);//合并单元格

// Range range1 = m_objWorkSheet.get_Range("A7", "A9");//选择操作块
Microsoft.Office.Interop.Excel.Range range8 = m_objExcel.get_Range("A7", "A9");//(m_objExcel.Cells[7, 1], m_objExcel.Cells[9,1])也行
//range8.HorizontalAlignment = XlHAlign.xlHAlignLeft; //设置文本水平居左 成功
//Range range8 = m_objWorkSheet.get_Range("A2", "E8");//选择操作块

range8.Font.Bold = true;//设置黑体

range8.Font.Size = 15;//设置字体大小

range8.Font.Name = "仿宋";//设置字体

range8.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue);//设置字体颜色

range8.HorizontalAlignment = XlHAlign.xlHAlignLeft;//设置水平对齐方式

range8.VerticalAlignment = XlVAlign.xlVAlignCenter;//设置垂直对齐方式

//////////////行////////////////////////////////////////////////////////////////////////////////////////
m_objExcel.get_Range(m_objExcel.Cells[5, 2], m_objExcel.Cells[5, 7]).Font.Size = 15;
Microsoft.Office.Interop.Excel.Range range2 = m_objExcel.get_Range(m_objExcel.Cells[5, 2], m_objExcel.Cells[5, 4]);
range2.Merge(Type.Missing);//合并单元格
Microsoft.Office.Interop.Excel.Range range3 = m_objExcel.get_Range(m_objExcel.Cells[5, 5], m_objExcel.Cells[5, 7]);
range3.Merge(Type.Missing);//合并单元格
Microsoft.Office.Interop.Excel.Range range4 = m_objExcel.get_Range(m_objExcel.Cells[5, 1], m_objExcel.Cells[6, 1]);
range4.Merge(Type.Missing);//合并单元格
m_objExcel.get_Range(m_objExcel.Cells[5, 2], m_objExcel.Cells[5, 7]).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);//设置字体颜色
m_objExcel.get_Range(m_objExcel.Cells[5, 2], m_objExcel.Cells[5, 2]).Value2 = "收入";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[5, 5], m_objExcel.Cells[5, 5]).Value2 = "支出";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[5, 1], m_objExcel.Cells[6, 1]).Value2 = "债务人";//给合并的单元格赋值
// m_objExcel.Cells[4, 5] = "单位:万元";
range1.Merge(Type.Missing);//合并单元格

m_objExcel.get_Range(m_objExcel.Cells[6, 2], m_objExcel.Cells[6, 2]).Value2 = "合计";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[6, 3], m_objExcel.Cells[6, 3]).Value2 = "上年转借数";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[6, 4], m_objExcel.Cells[6, 4]).Value2 = "当年举借数";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[6, 5], m_objExcel.Cells[6, 5]).Value2 = "合计";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[6, 6], m_objExcel.Cells[6, 6]).Value2 = "当年支出数";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[6, 7], m_objExcel.Cells[6, 7]).Value2 = "结转下年数";//给合并的单元格赋值

///////列//////////////////////////////////////////////////////////////////////////////////////////////

m_objExcel.get_Range(m_objExcel.Cells[7, 1], m_objExcel.Cells[7, 1]).Value2 = "合计";//给合并的单元格赋值
m_objExcel.get_Range(m_objExcel.Cells[8, 1], m_objExcel.Cells[8, 1]).Value2 = "一.直接债务";//给合并的单元格赋值

///////////////////////////////////////////////////////////////////////////////

m_objExcel.get_Range(m_objExcel.Cells[1, 2], m_objExcel.Cells[1, 4]).Font.Size = 20;
//m_objExcel.Cells[1, 1] = "建筑节能评估计算报告书";
m_objExcel.get_Range(m_objExcel.Cells[1, 1], m_objExcel.Cells[9,7]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);//设置边框颜色

m_objExcel.get_Range(m_objExcel.Cells[1, 1], m_objExcel.Cells[4, 1]).ColumnWidth = 40;//设置宽度.get_Range(excel.Cells[1, 1], excel.Cells[4, 1]).ColumnWidth = 20;//设置宽度

m_objExcel.get_Range(m_objExcel.Cells[5, 2], m_objExcel.Cells[5, 7]).ColumnWidth = 18;//设置宽度

//m_objExcel.Cells.Columns.HorizontalAlignment = XlHAlign.xlHAlignCenter; //设置文本水平居中 成功

///range1.VerticalAlignment = XlVAlign.xlVAlignCenter;//设置垂直对齐方式

range1.Font.Bold = true;//粗体
range1.Interior.ColorIndex = 15;//背景颜色
range1.EntireRow.AutoFit();//自动调整行高
// range1.Merge(Type.Missing);//合并单元格

}

public object xlCenter { get; set; }
}

// range1.HorizontalAlignment = m_objExcel.XlHAlign.xlHAlignCenter;//居中 不行
//m_objExcel.Cells.Columns.HorizontalAlignment = m_objExcel.XlHAlign.xlHAlignCenter; //不行