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;
}