xml 导入
public static System.Data.DataTable GetXmlTable(string fileName)
{
System.Data.DataSet dataSetXml;
System.Data.DataTable dataTableXml;
dataSetXml = new DataSet();
dataTableXml = new System.Data.DataTable();
try
{
dataSetXml.ReadXml(fileName);
dataTableXml = dataSetXml.Tables[1];
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return dataTableXml;
}
xml导出
protected void ReadDataSetToXML(DataSet ds, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
XmlTextWriter xtw = new XmlTextWriter(fs, System.Text.Encoding.UTF8);
ds.WriteXml(xtw, XmlWriteMode.IgnoreSchema);
xtw.Close();
fs.Close();
}
Excel导入
public static System.Data.DataTable GetExcelTable(string fileName, int dataStartLine, int nameLine, int columnCount)
{
System.Data.DataTable dtRet = null;
DataRow dr;
Application app = null;
Workbook wb = null;
Worksheet ws;
Range range;
object objMiss = null;
int iRow;
StringBuilder sbTemp;
string sTemp = string.Empty;
Console.WriteLine(DateTime.Now.ToLongTimeString() + " 正在读取Excel数据");
objMiss = System.Reflection.Missing.Value;
try
{
sbTemp = new StringBuilder();
dtRet = new System.Data.DataTable();
app = new Application();
app.DisplayAlerts = false;
app.Visible = false;
wb = app.Workbooks.Open(fileName, 1, true, objMiss, objMiss, objMiss, objMiss,
objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss, objMiss);
ws = (Worksheet)wb.Sheets[1];
for (int i = 1; i <= columnCount; i++)
{
range = (Range)ws.get_Range(num2Char(i) + nameLine, objMiss);
if (range.Formula.ToString().Length > 0)
{
dtRet.Columns.Add(range.Formula.ToString().Trim(), typeof(string));
}
else
{
columnCount--;
i--;
}
}
iRow = dataStartLine;
while (true)
{
dr = dtRet.NewRow();
sbTemp.Length = 0;
for (int i = 1; i <= columnCount; i++)
{
range = (Range)ws.get_Range(num2Char(i) + iRow, objMiss);
try
{
sTemp = range.Formula.ToString();
}
catch (System.Runtime.InteropServices.COMException)
{
sTemp = string.Empty;
}
sbTemp.Append(sTemp);
dr[i - 1] = sTemp;
}
if (sbTemp.Length > 0)
{
dtRet.Rows.Add(dr);
iRow++;
}
else
{
break;
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
wb.Close(false, objMiss, objMiss);
app.Quit();
app = null;
}
Console.WriteLine(DateTime.Now.ToLongTimeString() + " Excel数据读取完毕");
return dtRet;
}
Excel导出
protected void ExportOfExcel()
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=Publisher.xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。
//设置输出文件类型为excel文件。
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
Access,FoxPro等导入
private static System.Data.DataTable getOleDbTable(string tableName, string fileName, string connString)
{
OleDbDataAdapter oda;
string sql;
System.Data.DataTable dtRet = new System.Data.DataTable();
sql = "select * from " + tableName;
oda = new OleDbDataAdapter(sql, connString);
oda.Fill(dtRet);
return dtRet;
}
XML,Excel导入导出总结
原创
©著作权归作者所有:来自51CTO博客作者Terry_wang1983的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:vs2005快捷键
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java Excel导入导出
Java的两种方式操作Excel
数据 java excel 导出数据 -
【open】java导出excel【xml格式】excel xml github java i++
-
导出Excel
导出Excel
apache i++ java html 文件名 -
templates导出excel样式 导出excel模板
业务场景最近的工作中,会比较频繁的使用到导出功能,封装工具类后,通过简单的模板创建,就可以实现导出业务:一、环境配置pom引入<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId>
templates导出excel样式 excel java 阿里云 spring boot