open xml error : The given path's format is not supported
open xml error : The given path's format is not supported
我正在尝试使用 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))
错误:
An exception of type 'System.NotSupportedException' occurred in mAn
exception of type 'System.NotSupportedException' occurred in
mscorlib.dll but was not handled in user code
Additional information: The given path's format is not supported.
变量目的地有值:
IncorrectRecordsUploaded_9/9/2015 9:48:23 AM.xlsx
我该如何解决这个错误?
我试过了:
destination.replace("/","//");
但该行出现了同样的错误。
当我将文件重命名为 IncorrectRecordsUploaded.xlsx
时,它开始向我抛出 :
An exception of type 'System.UnauthorizedAccessExAn exception of type 'System.UnauthorizedAccessException' occurred in WindowsBase.dll but was not handled in user code
Additional information: Access to the path 'C:\Program Files (x86)\IIS Express\IncorrectRecordsUploaded.xlsx' is denied.
您描述了两个不同的错误。
第一个错误 由于文件名字符无效。
有几个符号,不能命名:<
、>
、:
、"
、/
、\
、|
、?
、*
。您也可以在 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
或 C:/
)。
我正在尝试使用 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))
错误:
An exception of type 'System.NotSupportedException' occurred in mAn exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code
Additional information: The given path's format is not supported.
变量目的地有值:
IncorrectRecordsUploaded_9/9/2015 9:48:23 AM.xlsx
我该如何解决这个错误?
我试过了:
destination.replace("/","//");
但该行出现了同样的错误。
当我将文件重命名为 IncorrectRecordsUploaded.xlsx
时,它开始向我抛出 :
An exception of type 'System.UnauthorizedAccessExAn exception of type 'System.UnauthorizedAccessException' occurred in WindowsBase.dll but was not handled in user code
Additional information: Access to the path 'C:\Program Files (x86)\IIS Express\IncorrectRecordsUploaded.xlsx' is denied.
您描述了两个不同的错误。
第一个错误 由于文件名字符无效。
有几个符号,不能命名:<
、>
、:
、"
、/
、\
、|
、?
、*
。您也可以在 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
或 C:/
)。