2024年3月6日发(作者:肖华灿)
第一种:将DataGrid中的数据以流的形式写到excel中,格式以html的形式存在
Response.Clear();
Response.Buffer = true;
Responsrset = "GB2312";
ResponpendHeader("Content-Disposition",
"attachment;filename=DialoutTemplate.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
ResponntentEncoding = Systcodin8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//ResponstentType = "application/v-excel";//输出类型
//Responarset = "";
//关闭 ViewState
EnableViewState = false;
System.IO.StringWriter tw = new Syst.StringWriter();//将信息写入字符串
System.Web.mlTextWriter hw = new
System.W.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
//此类提供AT服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
//获取control的HTML
dg.RenderControl(hw);//将table中的内容输出到HtmlTextWriter对象中
// 把HTML写回浏览器
Response.Write(tw.ToString());
Responush();
Respond();
第二种:将数据源中的数据以文件流的形式写到excel中,格式以txt的形式存在
FileStream fs = new
FileStream(ServePath("report_export/DialoutTemplate.xls"),
FileModate, FileAccess.Write);
StreamWriter rw = new StreamWriter(fs, Encoding.Default);//建立StreamWriter为写作准备;
DataTable dt = GetDataTableSource();
int count = dt.Columnnt;
string head = "";
string values = "";
for (int i = 0; i < count; i++)
{
string h = dt.Columns[i].ColumnName + "t";
string v = dt.Rows[0][i].ToString() + "t";
head += h;
values += v;
}
rw.WriteLine(head);
iteLine(values);
ose();
fse();
Respondirect("report_export/DialoutTemplas");
第三种:将数据源中的数据直接写到excel中,格式以xls形式存在,好处导出的
数据可以直接导入,可以将数字格式自动转化为文本格式,可以减少
格式转化的繁琐环节,还可以预留将数字转换为文本的格式的行数,
可以完全自定义
Excel.Application xlApp;
Excel.Workbook xlBook;
Excel.Workbooks xlBooks;
//Excel.Range xlRange;
Exceets xlsheets;
Excrksheet xlSheet;
int k = 0;
try
{
string strCurrentPath = ServePath("report_export/DialoutTemplate.xls");
string FilePath = strCurrentPath;
FileInfo fi = new FileInfo(FilePath);
if (fi.Exists) //判断文件是否已经存在,如果存在就删除!
{
fi.Delete();
}
xlApp = new Excel.Application();
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Add(Type.Missing);
xlsheets = xlBorksheets;
IntPtr intptr = new IntPtr(xlApp.Hwnd);
xlSheet = (Excel.Worksheet)xlsheet_Item(1);
DataTable dt = GetDataTableSource();
int count = dumns.Count;
for (int i = 0; i < count; i++)
{
string h = dumns[i].ColumnName;
string v = dt.Rows[0][i].ToString();
((Excel.Range)xlSheet.Cells[1, i + 1]).Value2 = h;
Excel.Range r1 = xlSheet.get_Range(xlSheels[1, 1], xlShells[1, i +
1]);
r1.NumberFormatLocal = "@";
((Excege)xlSheet.Cells[2, i + 1]).Value2 = v;
Excege r2 = xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[2, i +
1]);
r2.NumberFormatLocal = "@";
}
for (int j = 1; j < 500; j++)
{
Excel.Range r = xlShet_Range(xlSheet.Cells[2 + j, 1], xlSheet.Cells[2 +
j, count]);
r.NumberFormatLocal = "@";
}
xlBooeAs(FilePath, Type.Missing, Tyssing, Typsing,
Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Tyssing, Typsing);
xlBook.Close(false, Tyssing, Typsing);
xlBooks.Close();
xlApt();
Response.Redirect("report_export/DialoutTemplate.xls");
GetWindowThreadProcessId(intptr, out k);
Systegnostics.Process p =
Systagnostics.ProcetProcessById(k);
p.Kill();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
//xlRange = null;
xlSheet = null;
xlBook = null;
xlApp = null;
}
2024年3月6日发(作者:肖华灿)
第一种:将DataGrid中的数据以流的形式写到excel中,格式以html的形式存在
Response.Clear();
Response.Buffer = true;
Responsrset = "GB2312";
ResponpendHeader("Content-Disposition",
"attachment;filename=DialoutTemplate.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
ResponntentEncoding = Systcodin8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//ResponstentType = "application/v-excel";//输出类型
//Responarset = "";
//关闭 ViewState
EnableViewState = false;
System.IO.StringWriter tw = new Syst.StringWriter();//将信息写入字符串
System.Web.mlTextWriter hw = new
System.W.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
//此类提供AT服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
//获取control的HTML
dg.RenderControl(hw);//将table中的内容输出到HtmlTextWriter对象中
// 把HTML写回浏览器
Response.Write(tw.ToString());
Responush();
Respond();
第二种:将数据源中的数据以文件流的形式写到excel中,格式以txt的形式存在
FileStream fs = new
FileStream(ServePath("report_export/DialoutTemplate.xls"),
FileModate, FileAccess.Write);
StreamWriter rw = new StreamWriter(fs, Encoding.Default);//建立StreamWriter为写作准备;
DataTable dt = GetDataTableSource();
int count = dt.Columnnt;
string head = "";
string values = "";
for (int i = 0; i < count; i++)
{
string h = dt.Columns[i].ColumnName + "t";
string v = dt.Rows[0][i].ToString() + "t";
head += h;
values += v;
}
rw.WriteLine(head);
iteLine(values);
ose();
fse();
Respondirect("report_export/DialoutTemplas");
第三种:将数据源中的数据直接写到excel中,格式以xls形式存在,好处导出的
数据可以直接导入,可以将数字格式自动转化为文本格式,可以减少
格式转化的繁琐环节,还可以预留将数字转换为文本的格式的行数,
可以完全自定义
Excel.Application xlApp;
Excel.Workbook xlBook;
Excel.Workbooks xlBooks;
//Excel.Range xlRange;
Exceets xlsheets;
Excrksheet xlSheet;
int k = 0;
try
{
string strCurrentPath = ServePath("report_export/DialoutTemplate.xls");
string FilePath = strCurrentPath;
FileInfo fi = new FileInfo(FilePath);
if (fi.Exists) //判断文件是否已经存在,如果存在就删除!
{
fi.Delete();
}
xlApp = new Excel.Application();
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Add(Type.Missing);
xlsheets = xlBorksheets;
IntPtr intptr = new IntPtr(xlApp.Hwnd);
xlSheet = (Excel.Worksheet)xlsheet_Item(1);
DataTable dt = GetDataTableSource();
int count = dumns.Count;
for (int i = 0; i < count; i++)
{
string h = dumns[i].ColumnName;
string v = dt.Rows[0][i].ToString();
((Excel.Range)xlSheet.Cells[1, i + 1]).Value2 = h;
Excel.Range r1 = xlSheet.get_Range(xlSheels[1, 1], xlShells[1, i +
1]);
r1.NumberFormatLocal = "@";
((Excege)xlSheet.Cells[2, i + 1]).Value2 = v;
Excege r2 = xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[2, i +
1]);
r2.NumberFormatLocal = "@";
}
for (int j = 1; j < 500; j++)
{
Excel.Range r = xlShet_Range(xlSheet.Cells[2 + j, 1], xlSheet.Cells[2 +
j, count]);
r.NumberFormatLocal = "@";
}
xlBooeAs(FilePath, Type.Missing, Tyssing, Typsing,
Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Tyssing, Typsing);
xlBook.Close(false, Tyssing, Typsing);
xlBooks.Close();
xlApt();
Response.Redirect("report_export/DialoutTemplate.xls");
GetWindowThreadProcessId(intptr, out k);
Systegnostics.Process p =
Systagnostics.ProcetProcessById(k);
p.Kill();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
//xlRange = null;
xlSheet = null;
xlBook = null;
xlApp = null;
}