using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.IO;
namespace TopAPI
{
class ExcelCreate
{
public void CreateCsv(System.Data.DataTable dt, string strName)
{
//先打印标头
StringBuilder strColu=new StringBuilder();
StringBuilder strValue=new StringBuilder();
int i=0;
try
{
StreamWriter sw = new StreamWriter(new FileStream(strName+".csv", FileMode.Create), Encoding.GetEncoding("GB2312"));
for( i=0;i <workbook xmlns:o='/"urn:schemas-microsoft-com:office:office/"' microsoft-com:office: xmlns:ss='/"urn:schemas-microsoft-com:"'><styles> " +
"<style ss:id='/"Default/"' ss:name='/"Normal/"'> " +
"<Alignment ss:Vertical=/"Bottom/"/> <Borders/>" +
" <Font/> <Interior/> <NumberFormat/>" +
" <Protection/> </style> " +
"<style ss:id='/"BoldColumn/"'> <Font " +
"x:Family=/"Swiss/" ss:Bold=/"1/"/> </style> " +
"<style ss:id='/"StringLiteral/"'> <NumberFormat" +
" ss:Format=/"@/"/> </style>
<style> <NumberFormat " +
"ss:Format=/"0.0000/"/> </style> " +
"<style ss:id='/"Integer/"'> <NumberFormat " +
"ss:Format=/"0/"/> </style>
<style> <NumberFormat " +
"ss:Format=/"mm/dd/yyyy;@/"/> </style> " +
"</styles> ";
const string endExcelXML = "</workbook>";
int rowCount = 0;
int sheetCount = 1;
excelDoc.Write(startExcelXML);
excelDoc.Write("<worksheet ss:name='/"Sheet"' sheetcount>");
excelDoc.Write("<table>");
excelDoc.Write("<row>");
for (int x = 0; x <data ss:type='/"String/"'>");
excelDoc.Write(source.Columns[x].ColumnName);
excelDoc.Write("</data>");
}
excelDoc.Write("</row>");
foreach (DataRow x in source.Rows)
{
rowCount++;
//if the number of rows is > 64000 create a new page to continue output
if (rowCount == 64000)
{
rowCount = 0;
sheetCount++;
excelDoc.Write("</table>");
excelDoc.Write(" </worksheet>");
excelDoc.Write("<worksheet ss:name='/"Sheet"' sheetcount>");
excelDoc.Write("<table>");
}
excelDoc.Write("<row>"); //ID=" + rowCount + "
for (int y = 0; y ", ">");
XMLstring = XMLstring.Replace("" +
"<data ss:type='/"String/"'>");
excelDoc.Write(XMLstring);
excelDoc.Write("</data>");
break;
case "System.DateTime":
//Excel has a specific Date Format of YYYY-MM-DD followed by
//the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
//The Following Code puts the date stored in XMLDate
//to the format above
DateTime XMLDate = (DateTime)x[y];
string XMLDatetoString = ""; //Excel Converted Date
XMLDatetoString = XMLDate.Year.ToString() +
"-" +
(XMLDate.Month " +
"<data ss:type='/"DateTime/"'>");
excelDoc.Write(XMLDatetoString);
excelDoc.Write("</data>");
break;
case "System.Boolean":
excelDoc.Write("<cell ss:styleid='/"StringLiteral/"'>" +
"<data ss:type='/"String/"'>");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</data></cell>");
break;
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Byte":
excelDoc.Write("<cell ss:styleid='/"Integer/"'>" +
"<data ss:type='/"Number/"'>");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</data></cell>");
break;
case "System.Decimal":
case "System.Double":
excelDoc.Write("<cell ss:styleid='/"Decimal/"'>" +
"<data ss:type='/"Number/"'>");
excelDoc.Write(x[y].ToString());
excelDoc.Write("</data></cell>");
break;
case "System.DBNull":
excelDoc.Write("<cell ss:styleid='/"StringLiteral/"'>" +
"<data ss:type='/"String/"'>");
excelDoc.Write("");
excelDoc.Write("</data></cell>");
break;
default:
throw (new Exception(rowType.ToString() + " not handled."));
}
}
excelDoc.Write("</row>");
}
excelDoc.Write("</table>");
excelDoc.Write(" </worksheet>");
excelDoc.Write(endExcelXML);
excelDoc.Close();
}
}
}
分享到:
相关推荐
Javascript导出excel为xlsx格式,兼容IE6+和主流浏览器,下载下来直接可以使用。 其他javascript导出excel插件可看作者文章:https://blog.csdn.net/qq_21693027/article/details/80459677
java导出CVS,csv只是一种文件的形式。。。就是按照逗号(英文逗号)分隔的文件 它的做成跟普通的文件做成是一样一样的,只需要创建一个文件(*.csv),把字符串按照一定的格式(AAA,BBB\nCCC,DDD注意必须是英文的...
PHP导出Excel (注:导出带图片的EXCEL也支持),已修正保存到本地不能打开已经乱码的问题,具体内容可参照文件中demo.php有详细说明。
导入导出excel2003、2007、cvs 的demo
js将html中table导出Excel,CVS格式
C# excel CVS等内容,详细且精准
普通的excel导出都是经过ole或者DataGridView不够灵活,在这个项目中我采用了cvs的方式写入excel,更加灵活简单!一个个简单的excel类库,自己模仿dom4j封装了几个类,导出excel文件。自己在项目中写了一个小的demo,...
java导出CVS,csv只是一种文件的形式。。。就是按照逗号(英文逗号)分隔的文件 它的做成跟普通的文件做成是一样一样的,只需要创建一个文件(*.csv),把字符串按照一定的格式(AAA,BBB\nCCC,DDD注意必须是英文的...
怎么定时自动将SQL数据导出为Excel文件
PHP把数据导出为CVS格式的类This class can pull only the specified fields from a csv file. you can specify either the field numbers or field names to pull out. Also it can just show you a numbered list ...
采用POI、JXL框架导出CVS文件,支持千万级数据导出,无内存溢出,自己项目中使用中。
csdn已有,导出cvs的也整理了下,一起发上来
支持从数据库中查询到数据,以多级表头形式导入到Excel文件中。比如:一级表头:学号、姓名、各科成绩。二级表头:语文、数学、英语(二级表头在各科成绩下面)。
InfluxDB查询 导出数据 mybatis SpringBoot
csvfielddumpPHP把数据导出为CVS格式的类.rar
用流的方法导出数据集到Excel,非常快
非常好用的,速度超快,免费的。请支持原作者,微博名:云计算民工。我只是搬运工。教程请自行去搜。
DataTables导出CVS Excel PDF 打印
使用cvs和数据库把excel里的数据整理成想要的格式,开发中快捷格式批量数据
轻松解决普通poi形式导出Excel的中出现的栈溢出问题,此资源可实现千万级数据分批导出csv文件,测试实现16500000条数据大概80秒左右;具体表里内容。