读取一个特殊的文件存在DataTable里

s208ping 2010-09-17 09:55:44
文件内容:

<? xml version="1.0" ?>
<xliff version="1.0">
<file original="sample.html"
source-language="en"
datatype="HTML Page">
<header>
<skl>
<external-file href="sample.skl"/>
</skl>
</header>
<body>
<trans-unit id="%%%1%%%">
<source xml:lang="en">
中文1</source>
</trans-unit>
<trans-unit id="%%%2%%%">
<source xml:lang="en">
中文2</source>
</trans-unit>
<trans-unit id="%%%3%%%">
<source xml:lang="en">
中文3</source>
</trans-unit>
</body>
</file>
</xliff>



怎么读出 中文1,中文2等等等.。。。

然后

public static datatable dt()
{
.............
return dt
}



多谢多谢
...全文
75 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyerwing 2010-09-17
  • 打赏
  • 举报
回复
JS也可以做到了.
C井也可以能了,不难了.就是麻烦
createTim 2010-09-17
  • 打赏
  • 举报
回复
你要改一下

public static DataTable XMLDataTable3(XmlNodeList nodelist)
{
DataTable table = null;
if (null == nodelist)
return null;
if (0 == nodelist[0].ChildNodes.Count)
return null;
// create data table
table = new DataTable();
for (int i = 0; i < nodelist[0].ChildNodes.Count;i++ )
{
table.Columns.Add(nodelist[0].ChildNodes[i].Name);
}

// add rows
DataRow row = null;
foreach (XmlNode n in nodelist)
{
row = table.NewRow();
foreach (XmlNode a in n.ChildNodes)
{
row[a.Name] = a.InnerText;
}
table.Rows.Add(row);
}

table.AcceptChanges();
return table;
}
引用之后,使用该方法读取excel文件 #region 读取Excel public static DataTable ImportExcel(string filePath) { try { //根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档 FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //判断文件后缀名是xls,还是xlsx,如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook XSSFWorkbook workbook = new XSSFWorkbook(file); //获取excel的第一个sheet ISheet sheet = workbook.GetSheetAt(0); DataTable table = new DataTable(); //获取sheet的首行 IRow headerRow = sheet.GetRow(0); if (headerRow == null) headerRow = sheet.GetRow(1); //一行最后一个方格的编号 即总的列数 int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { ICell cell = headerRow.GetCell(i); if (cell != null) { DataColumn column = new DataColumn(cell.StringCellValue); table.Columns.Add(column); } } //最后一列的标号 即总的行数 // int rowCount = sheet.LastRowNum; cellCount = table.Columns.Count; for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row == null) continue; DataRow dataRow = table.NewRow(); bool isAdd = false; for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) == null) continue; //row.GetCell(j).SetCellType(CellType.STRING); if (row.GetCell(j) != null) { if (row.GetCell(j).CellType == CellType.NUMERIC) dataRow[j] = (int)(row.GetCell(j).NumericCellValue); else dataRow[j] = row.GetCell(j).StringCellValue; isAdd = true; } } if (isAdd) table.Rows.Add(dataRow); } workbook = null; sheet = null; return table; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return null; } } #endregion
对学习wpf的朋友们有一定的帮助 /// /// 工参表分割类, 按区域进行对数据行进行分割 /// public class AreaSplit { public event EventHandler Opening; public event EventHandler ErrorHappened; public event EventHandler BeginSplit; public event EventHandler Splitting; public event EventHandler Saving; public event EventHandler Splitted; private List m_ColNames; /// /// 执行工参表分割 /// /// 原始工参文件 /// 分割后输出所在总目录 public void StartSplit(string sourceFile, string outPath) { try { DictionaryDataTable> dict = this.SplitToDictionary(sourceFile); if (dict == null) return; this.SaveToExcels(outPath, dict); } catch (Exception ex) { if (ErrorHappened != null) ErrorHappened(this, new StringEventArgs(ex.Message)); } } /// /// 将工参文件分割为DictionaryDataTable> /// private DictionaryDataTable> SplitToDictionary(string sourceFile) { if (!File.Exists(sourceFile)) { if (ErrorHappened != null) ErrorHappened(this, new StringEventArgs("指定路径的文件存在")); return null; } if (this.Opening != null) this.Opening(this, new StringEventArgs("正在读取原始工参表 ...")); Workbook wb = new Workbook(sourceFile); if (wb.Worksheets.Count == 0) { if (ErrorHappened != null) ErrorHappened(this, new StringEventArgs("缺少有效的工作表")); return null; } Worksheet ws = wb.Worksheets[0]; string areaKey = "区域"; int areaPos = -1; // 获取区域所在列号 this.m_ColNames = new List(); int rowCount = ws.Cells.MaxDataRow + 1; int colCount = ws.Cells.MaxDataColumn + 1; for (int i = 0; i < colCount; i++) { string str = ws.Cells[0, i].StringValue; if (str == null) continue; if (str.Equals(areaKey)) { areaPos = i; } this.m_ColNames.Add(str); } if (areaPos == -1) { if (ErrorHappened != null) ErrorHappened(this, new StringEventArgs("找不到正确的区域列")); return null; } if (BeginSplit != null) BeginSplit(this, new Int32EventArgs(rowCount)); DictionaryDataTable> dict = new DictionaryDataTable>(); for (int i = 1; i < rowCount; i++) { string area = ws.Cells[i, areaPos].StringValue; if (string.IsNullOrEmpty(area)) area = "未知区域"; if (!dict.ContainsKey(area)) { DataTable dt = this.GetTable(colCount); dict.Add(area, dt); } DataRow row = dict[area].NewRow(); for (int j = 0; j < colCount; j++) { row[j] = ws.Cells[i, j].StringValue; } dict[area].Rows.Add(row); if (Splitting != null) Splitting(this, new Int32EventArgs(i)); } return dict; } /// /// 将分割后的数据,按区域分别保存为Excel文档 /// /// /// private void SaveToExcels(string outPath, DictionaryDataTable> dict) { foreach (string key in dict.Keys) { if (Saving != null) this.Saving(this, new StringEventArgs("正在保存 " + key + "工参表")); DataTable dt = dict[key]; if (outPath.Equals("")) outPath = AppDomain.CurrentDomain.BaseDirectory + "分割后工参"; string dir = outPath + "\\" + key; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); string fn = dir + "\\" + key + "_" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx"; if (File.Exists(fn)) File.Delete(fn); Workbook wb = new Workbook(); wb.Worksheets.Clear(); Worksheet ws = wb.Worksheets.Add(key + "工参表"); for (int i=0; iDataTable GetTable(int colCount) { DataTable dt = new DataTable(); for (int i = 0; i < colCount; i++) { DataColumn col = new DataColumn(); col.DataType = typeof(string); dt.Columns.Add(col); } return dt; }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧