以前的vs05 winform程序错误信息
1.vshost.exe 已停止工作。
2.导出excel时 result 0x800AC472 错误。有没有具体的解决办法 谢谢了~!
1.vshost.exe 已停止工作。
2.导出excel时 result 0x800AC472 错误。有没有具体的解决办法 谢谢了~!
解决方案 »
- 关于控件的name属性
- C#中,为什么默认的新建窗体Form1是类,但是不用实例化这个类我们就能见到生成后的对象Form1呢?
- 高手的来
- 在c#中插入flash动画的问题?
- ApplicationException和Exception
- 在 C# 中怎样来固定住一个ListView控件的列头的宽度.让用户不能改变它.只是其中的一列,另外的还可以改变.
- 求硕士计算机类能发表论文的刊物,给要发表文章的兄弟帮帮忙!!!!
- C# DataTable 分类汇总
- 那位大侠有关C#做的管理系统,有WINFORM版及WEBFORM
- c#开发数据库如何?
- 新手的关于引用子类的问题。
- 【简单问题】应该如何向网站Post数据
/// 将传入的DataSet数据导出至Excel文件
/// </summary>
/// <param name="ctl">DataGrid</param>
public static void DataSet2Excel(DataSet ds)
{
int maxRow=ds.Tables[0].Rows.Count;
string fileName=DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";//设置导出文件的名称 DataView dv=new DataView(ds.Tables[0]);//将DataSet转换成DataView
string fileURL=string.Empty;
//调用方法将文件写入服务器,并获取全部路径
fileURL=DataView2ExcelBySheet(dv,fileName);
//获取路径后从服务器下载文件至本地
HttpContext curContext=System.Web.HttpContext.Current;
curContext.Response.ContentType="application/vnd.ms-excel";
curContext.Response.ContentEncoding=System.Text.Encoding.Default;
curContext.Response.AppendHeader("Content-Disposition", ("attachment;filename=" + fileName));
curContext.Response.Charset = ""; curContext.Response.WriteFile(fileURL);
curContext.Response.Flush();
curContext.Response.End();
} /// <summary>
/// 分Sheet导出Excel文件
/// </summary>
/// <param name="dv">需导出的DataView</param>
/// <returns>导出文件的路径</returns>
private static string DataView2ExcelBySheet(DataView dv,string fileName)
{
int sheetRows=65535;//设置Sheet的行数,此为最大上限,本来是65536,因表头要占去一行
int sheetCount = (dv.Table.Rows.Count - 1) / sheetRows + 1;//计算Sheet数 GC.Collect();//垃圾回收 Application excel;
_Workbook xBk;
_Worksheet xSt=null;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
//定义循环中要使用的变量
int dvRowStart;
int dvRowEnd;
int rowIndex = 0;
int colIndex = 0;
//对全部Sheet进行操作
for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
{
//初始化Sheet中的变量
rowIndex = 1;
colIndex = 1;
//计算起始行
dvRowStart = sheetIndex * sheetRows;
dvRowEnd = dvRowStart + sheetRows-1;
if (dvRowEnd > dv.Table.Rows.Count-1)
{
dvRowEnd = dv.Table.Rows.Count - 1;
}
//创建一个Sheet
if (null == xSt)
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
}
else
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, xSt, 1, Type.Missing);
}
//设置Sheet的名称
xSt.Name = "Expdata";
if (sheetCount > 1)
{
xSt.Name += ((int)(sheetIndex + 1)).ToString();
}
//取得标题
foreach (DataColumn col in dv.Table.Columns)
{
//设置标题格式
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //设置标题居中对齐
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;//设置标题为粗体
//填值,并进行下一列
excel.Cells[rowIndex, colIndex++] = col.ColumnName;
}
//取得表格中数量
int drvIndex;
for(drvIndex=dvRowStart;drvIndex<=dvRowEnd;drvIndex++)
{
DataRowView row=dv[drvIndex];
//新起一行,当前单元格移至行首
rowIndex++;
colIndex = 1;
foreach (DataColumn col in dv.Table.Columns)
{
if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
}
else if (col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
colIndex++;
}
}
//使用最佳宽度
Range allDataWithTitleRange = xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex, colIndex-1]);
allDataWithTitleRange.Select();
allDataWithTitleRange.Columns.AutoFit();
allDataWithTitleRange.Borders.LineStyle = 1;//将导出Excel加上边框
}
//设置导出文件在服务器上的文件夹
string exportDir="~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行
//设置文件在服务器上的路径
string absFileName = HttpContext.Current.Server.MapPath(System.IO.Path.Combine(exportDir,fileName));
xBk.SaveCopyAs(absFileName);
xBk.Close(false, null, null);
excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xBk = null;
excel = null;
xSt = null;
GC.Collect();
//返回写入服务器Excel文件的路径
return absFileName;
}说明:如果您需要将导出文件保存到服务器,则只需采用DataView2ExcelBySheet方法即可,且不需返回路径,直接在服务器的该路径下即可找到文件.