我正在尝试使用open xml将给定的数据表导出到xlsx文件。
我写了以下代码:
private void ExportToExcelFileOpenXML(DataTable dt, string destination)
{
DataSet ds = new DataSet();
DataTable dtCopy = new DataTable();
dtCopy = dt.Copy();
ds.Tables.Add(dtCopy);
using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
foreach (System.Data.DataTable table in ds.Tables)
{
var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
uint sheetId = 1;
if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
{
sheetId =
sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
sheets.Append(sheet);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<String> columns = new List<string>();
foreach (System.Data.DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (System.Data.DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach (String col in columns)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
}
}
在这个函数中,我收到错误:
using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
错误:
mAn中出现'System.NotSupportedException'类型的异常 发生了'System.NotSupportedException'类型的异常 mscorlib.dll但未在用户代码中处理
Additional information: The given path's format is not supported.
变量目标具有值:
IncorrectRecordsUploaded_9 / 9/2015 9:48:23 AM.xlsx
如何解决此错误?
我试过了:
destination.replace("/","//");
但同样的错误就在那条线上。
当我将文件重命名为IncorrectRecordsUploaded.xlsx
时,它开始抛弃我:
类型'System.UnauthorizedAccessExAn的异常'类型'System.UnauthorizedAccessException'的异常在WindowsBase.dll中发生但未在用户代码中处理
其他信息:拒绝访问路径“C:\ Program Files(x86)\ IIS Express \ IncorrectRecordsUploaded.xlsx”。
答案 0 :(得分:1)
你解决了两个不同的错误。
由于文件名字符无效而发生第一次错误。
有几个符号在命名时有所不同:<
,>
,:
,"
,/
,\
,{{1 },|
,?
。您也可以在MSDN上查看naming files and folders的规则。
如果您自己创建目标路径,则可以使用此正则表达式删除所有不允许的符号:
*
您的第二个错误发生,因为您无法访问此路径。尝试将文件保存到其他位置(using System.IO;
using System.Text.RegularExpressions;
var pattern = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
var r = new Regex(string.Format("[{0}]", Regex.Escape(pattern)));
tempFileName = r.Replace(tempFileName, "_");
或Documents
)。