最好是导出excel格式,这个都熟悉
解决方案 »
- 请教大家webbrowser对文本框赋值的问题
- 用sql server2008 如何数据库备份
- C# 复数Excel中取值,怎样把合计写到新Excle中?
- BOM 添加和删除操作的一点疑惑
- 编程模拟网页登陆问题
- 怎么DrawString竖着写
- 使用 telnet 命令检查SQL Server 2005服务器工作状态时的问题
- 如何将一个 long 转换成一个byte[] ?
- 如何判断webbrowser 最后一次加载?
- 请问有没有显现能让用户选择关闭某tab的tabcontrol就象腾讯浏览器那样的东东
- Diagnostics.Contract,契约,有用过的朋友麻烦进来下
- 想问两个基本的问题,希望大家帮帮忙
通过界面输入条件(如姓名) 选出记录得到datatable并绑定到datagridview
然后导出到excel 当然也可以直接输入条件 得到datatable就直接导出
导出的代码如下/// <summary>
/// 导出数据到excel
/// </summary>
/// <param name="grid"></param>
/// <param name="ReportTitle"></param>
public static void ExportDataGridToExcel(DataTable myTable, string ReportTitle)
{
//DataTable myTable = (DataTable)grid.DataSource; try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); int rowIndex;
int colIndex; rowIndex = 1;
colIndex = 0; Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true); List<Microsoft.Office.Interop.Excel.Range> lirange = new List<Microsoft.Office.Interop.Excel.Range>();
Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[2, 6], xlApp.Cells[100, 14]);
//Range("A1:A1").SelectSelection.NumberFormatLocal = "@"
range.NumberFormatLocal = "@";// 设置单元格格式无
for (int i = 3; i < myTable.Rows.Count; i++)
{ Microsoft.Office.Interop.Excel.Range range1 = xlApp.get_Range(xlApp.Cells[i, 6], xlApp.Cells[i, 14]);
//Range("A1:A1").SelectSelection.NumberFormatLocal = "@"
range1.NumberFormatLocal = "@";// 设置单元格格式无
lirange.Add(range1);
}
//xlApp.ActiveCell.d
//xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
//xlApp.ActiveCell.Font.Size = 18;
//xlApp.ActiveCell.Font.Bold = true; //将表中的栏位名称填到Excel的第一行
for (int i = 0; i < myTable.Columns.Count; i++)
{
colIndex = colIndex + 1;
//myTable.Columns[i].DataType = typeof(string);
xlApp.Cells[1, colIndex] = myTable.Columns[i].ColumnName;
} //得到的表所有行,赋值给单元格
for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myTable.Columns.Count; col++)
{
colIndex = colIndex + 1;
if (myTable.Rows[row][col].ToString() == "==")
xlApp.Cells[rowIndex, colIndex] = " == ";// myTable.Rows[row][col].ToString();
else
{
xlApp.Cells[rowIndex, colIndex] = myTable.Rows[row][col].ToString();
}
}
} //xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
//xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1; xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter; xlApp.Visible = true;
}
catch (Exception e)
{
throw e;
} }
/// <summary>
/// 名称:WriteToExcel
/// 功能:查询结果导出Execl
/// 编写人:
/// 编写时间:2008-11-26
/// </summary>
/// <param name="table"></param>
public void WriteToExcel(DataTable table)
{
try
{
string tempImagePath = Application.StartupPath;
string temp = tempImagePath + "\\ExeclFiles";
Directory.CreateDirectory(@temp);
string strFilePath = @Application.StartupPath + @"\ExeclFiles\" + CommonClass.CommonDateTime() + ".xls";
System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath,true, System.Text.Encoding.Default);
object[] values = new object[table.Columns.Count];
for (int i = 0; i < table.Columns.Count; ++i)
{
sw.Write(table.Columns[i].Caption.ToString());
sw.Write('\t');
}
sw.Write("\r\n");
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < values.Length; ++j)
{
sw.Write(table.Rows[i][j].ToString());
sw.Write('\t');
}
sw.Write("\r\n");
}
sw.Flush();
sw.Close();
CommonClass.MessageBoxOK("成功导出[" + ds.Tables[0].Rows.Count.ToString() + "]行到Execl!");
}
catch
{
CommonClass.MessageBoxNo("导出Execl失败!");
}
}
晕:看来你觉的我是忽悠你的.我只知道我天天导出的都是用execl打开查看我快速闪过.
那个方法导出的文件是可以导回去
/// 将DATAGRID导出为EXCEL文件方法一,
/// 参数是:要导出的DATAGRID的ID和要保存下来的EXCEL文件名
/// </summary>
/// <param name="myPage">page</param>
/// <param name="dg">datagrid</param>
/// <param name="name">filename</param>
private void OutExcel(Page myPage,DataGrid dg,string name)
{
HttpResponse Response;
Response=myPage.Response; string name1="attachment;filename="+name+".xls";
dg.Visible=true;
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition",name1); Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType ="application/ms-excel"; dg.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); if (excel == null) { //MessageBox.Show("Excel无法启动", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } int rowindex = 2; int colindex = 0; excel.Application.Workbooks.Add(true); System.Data.DataTable dt = ds.Tables[0]; Microsoft.Office.Interop.Excel.Range range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, dt.Columns .Count]); range.MergeCells = true; excel.ActiveCell.FormulaR1C1 =fileStrName; excel.ActiveCell.Font.Size = 18; excel.ActiveCell.Font.Bold = true; foreach (DataColumn col in dt.Columns) { colindex=colindex +1; excel.Cells[2, colindex] = col.ColumnName; } foreach (DataRow row in dt.Rows) { colindex = 0; rowindex++; foreach (DataColumn col in dt.Columns) { colindex++; excel.Cells[rowindex, colindex] = row[col.ColumnName].ToString(); } } excel.get_Range(excel.Cells[1, 1], excel.Cells[1, dt.Columns.Count]).Font.Bold = true; excel.get_Range(excel.Cells[1, 1], excel.Cells[rowindex, colindex]).Borders.LineStyle = 0; excel.Cells.EntireColumn.AutoFit(); excel.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter; excel.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter; try {
excel.DisplayAlerts = false;
excel.Visible = false;
//excel.DisplayAlerts =false;
excel.AlertBeforeOverwriting = true;
excel.Save(fileStrName);
} catch { } finally { excel.Quit(); excel = null; } }
运行到 excel.Save(fileStrName);是总是弹出另存为对话框,怎么解决。