C#的excel汇出功能问题 真的好奇怪excel的设置问题? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那如果是excel的设置问题,该如何解决呢?楼上那位兄弟有何高见?/? 我想应该不是Excel的问题,你最好将Database中的数据完整显示出来,再查查原因! 给你个实例: /// <summary> /// 建立Excel /// </summary> public void CreateExcel() { int rowIndex=4;//行起始坐标 int colIndex=1;//列起始坐标 ApplicationClass myApp=null; Workbook myBook=null; Worksheet mySheet=null; //如果文件不存在,则将模板文件拷贝一份作为输出文件 //这里如果通过File.Create来创建文件是不行的,因为xls //的空文件也有固定的格式,跟文本不一样的,也许有其它 //通过程序直接生成excel的方法 if(!File.Exists(m_outFilePath)) { if((File.GetAttributes(m_inputFilePath) & FileAttributes.ReadOnly)==FileAttributes.ReadOnly) { File.SetAttributes(m_inputFilePath,FileAttributes.Archive); } File.Copy(m_inputFilePath,m_outFilePath,true); } if((File.GetAttributes(m_outFilePath) & FileAttributes.ReadOnly)==FileAttributes.ReadOnly) { File.SetAttributes(m_outFilePath,FileAttributes.Archive); } myApp= new ApplicationClass(); myApp.Visible=false; object oMissiong=System.Reflection.Missing.Value; myApp.Workbooks.Open(m_outFilePath,oMissiong,oMissiong,oMissiong,oMissiong, oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);//这个函数只适合于OFFICE2000回为它只有13个参数,要适应于OFFICE XP的话,得要15个参数 myBook=myApp.Workbooks[1]; mySheet=(Worksheet)myBook.ActiveSheet; // //取得标题 // foreach(DataColumn col in m_dv.Table.Columns) { colIndex++; mySheet.Cells[4,colIndex] = col.ColumnName; mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //设置标题格式为居中对齐 } // //取得表格中的数据 // foreach(DataRowView row in m_dv) { rowIndex ++; colIndex = 1; foreach(DataColumn col in m_dv.Table.Columns) { colIndex ++; if(col.DataType == System.Type.GetType("System.DateTime")) { mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd"); mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐 } else if(col.DataType == System.Type.GetType("System.String")) { mySheet.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString(); mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐 } else { mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString(); } } } // //加载一个合计行 // int rowSum = rowIndex + 1; int colSum = 2; // //设置选中的部分的颜色 // mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Select(); mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种 // //取得整个报表的标题 // mySheet.Cells[2,2] = m_title; // //设置整个报表的标题格式 // mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Bold = true; mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Size = 22; // //设置报表表格为最适应宽度 // mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Select(); mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Columns.AutoFit(); // //设置整个报表的标题为跨列居中 // mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).Select(); mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection; // //绘制边框 // mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Borders.LineStyle = 1; mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗 mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗 mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗 mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗 myBook.Save(); myBook.Close( true,m_outFilePath,true); System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp); GC.Collect(); } 呵呵,谢谢各位了。我也跟大家说明咋回事吧。DB中那个栏位只要给歌空格就ok了^_^ 搞什么东东, 是不是大妹子 跟 Crystal??? XmlDocument如何释放资源 请问,winform如何显示官网北京时间 如何在点击一个按钮控件的时候直接创建这个按钮的click的事件? 使用水晶报表的问题 求一个排序算法,请求高手帮忙 请教,在使用C#下载js文件时报404文件不存在的错误 如何把menu的click用代碼指給某button的click事件。 请教:消息队列跨机器发消息 或者 如何创建“公共队列” 程序问题 求助无边框窗口的缩放问题 有一个文件打开是乱码,出200分求解决方法.留下QQ或信箱我把文件发过去. 用户资料在数据库读出之后用什么储存?C/S结构好象没有Cookies或Session之类。
/// <summary>
/// 建立Excel
/// </summary>
public void CreateExcel()
{
int rowIndex=4;//行起始坐标
int colIndex=1;//列起始坐标
ApplicationClass myApp=null;
Workbook myBook=null;
Worksheet mySheet=null;
//如果文件不存在,则将模板文件拷贝一份作为输出文件
//这里如果通过File.Create来创建文件是不行的,因为xls
//的空文件也有固定的格式,跟文本不一样的,也许有其它
//通过程序直接生成excel的方法
if(!File.Exists(m_outFilePath))
{
if((File.GetAttributes(m_inputFilePath) & FileAttributes.ReadOnly)==FileAttributes.ReadOnly)
{
File.SetAttributes(m_inputFilePath,FileAttributes.Archive);
}
File.Copy(m_inputFilePath,m_outFilePath,true);
} if((File.GetAttributes(m_outFilePath) & FileAttributes.ReadOnly)==FileAttributes.ReadOnly)
{
File.SetAttributes(m_outFilePath,FileAttributes.Archive);
}
myApp= new ApplicationClass();
myApp.Visible=false;
object oMissiong=System.Reflection.Missing.Value;
myApp.Workbooks.Open(m_outFilePath,oMissiong,oMissiong,oMissiong,oMissiong,
oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);//这个函数只适合于OFFICE2000回为它只有13个参数,要适应于OFFICE XP的话,得要15个参数
myBook=myApp.Workbooks[1];
mySheet=(Worksheet)myBook.ActiveSheet; //
//取得标题
//
foreach(DataColumn col in m_dv.Table.Columns)
{
colIndex++;
mySheet.Cells[4,colIndex] = col.ColumnName;
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
//设置标题格式为居中对齐
}
//
//取得表格中的数据
//
foreach(DataRowView row in m_dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in m_dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
mySheet.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2; //
//设置选中的部分的颜色
//
mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
mySheet.Cells[2,2] = m_title;
//
//设置整个报表的标题格式
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Bold = true;
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
myBook.Save();
myBook.Close( true,m_outFilePath,true);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);
GC.Collect();
}