我需要將代碼導出到windows窗體應用程序中的excel ...任何人都可以使用該代碼?在Windowsforms應用中將gridview導入excel
0
A
回答
2
這是一個漫長的答案,但在這裏不言而喻。
嘗試創建下面的類...
public class ExcelWriter : IDisposable
{
private XmlWriter _writer;
public enum CellStyle { General, Number, Currency, DateTime, ShortDate };
public void WriteStartDocument()
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"");
_writer.WriteStartElement("ss", "Workbook", "urn:schemas-microsoft-com:office:spreadsheet");
WriteExcelStyles();
}
public void WriteEndDocument()
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteEndElement();
}
private void WriteExcelStyleElement(CellStyle style)
{
_writer.WriteStartElement("Style", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("ID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
_writer.WriteEndElement();
}
private void WriteExcelStyleElement(CellStyle style, string NumberFormat)
{
_writer.WriteStartElement("Style", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("ID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
_writer.WriteStartElement("NumberFormat", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("Format", "urn:schemas-microsoft-com:office:spreadsheet", NumberFormat);
_writer.WriteEndElement();
_writer.WriteEndElement();
}
private void WriteExcelStyles()
{
_writer.WriteStartElement("Styles", "urn:schemas-microsoft-com:office:spreadsheet");
WriteExcelStyleElement(CellStyle.General);
WriteExcelStyleElement(CellStyle.Number, "General Number");
WriteExcelStyleElement(CellStyle.DateTime, "General Date");
WriteExcelStyleElement(CellStyle.Currency, "Currency");
WriteExcelStyleElement(CellStyle.ShortDate, "Short Date");
_writer.WriteEndElement();
}
public void WriteStartWorksheet(string name)
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteStartElement("Worksheet", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("Name", "urn:schemas-microsoft-com:office:spreadsheet", name);
_writer.WriteStartElement("Table", "urn:schemas-microsoft-com:office:spreadsheet");
}
public void WriteEndWorksheet()
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteEndElement();
_writer.WriteEndElement();
}
public ExcelWriter(string outputFileName)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
_writer = XmlWriter.Create(outputFileName, settings);
}
public void Close()
{
if (_writer == null) throw new NotSupportedException("Already closed.");
_writer.Close();
_writer = null;
}
public void WriteExcelColumnDefinition(int columnWidth)
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteStartElement("Column", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteStartAttribute("Width", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteValue(columnWidth);
_writer.WriteEndAttribute();
_writer.WriteEndElement();
}
public void WriteExcelUnstyledCell(string value)
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteStartElement("Cell", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteStartElement("Data", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "String");
_writer.WriteValue(value);
_writer.WriteEndElement();
_writer.WriteEndElement();
}
public void WriteStartRow()
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteStartElement("Row", "urn:schemas-microsoft-com:office:spreadsheet");
}
public void WriteEndRow()
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteEndElement();
}
public void WriteExcelStyledCell(object value, CellStyle style)
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
_writer.WriteStartElement("Cell", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("StyleID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
_writer.WriteStartElement("Data", "urn:schemas-microsoft-com:office:spreadsheet");
switch (style)
{
case CellStyle.General:
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "String");
break;
case CellStyle.Number:
case CellStyle.Currency:
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "Number");
break;
case CellStyle.ShortDate:
case CellStyle.DateTime:
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "DateTime");
break;
}
_writer.WriteValue(value);
// tag += String.Format("{1}\"><ss:Data ss:Type=\"DateTime\">{0:yyyy\\-MM\\-dd\\THH\\:mm\\:ss\\.fff}</ss:Data>", value,
_writer.WriteEndElement();
_writer.WriteEndElement();
}
public void WriteExcelAutoStyledCell(object value)
{
if (_writer == null) throw new NotSupportedException("Cannot write after closing.");
//write the <ss:Cell> and <ss:Data> tags for something
if (value is Int16 || value is Int32 || value is Int64 || value is SByte ||
value is UInt16 || value is UInt32 || value is UInt64 || value is Byte)
{
WriteExcelStyledCell(value, CellStyle.Number);
}
else if (value is Single || value is Double || value is Decimal) //we'll assume it's a currency
{
WriteExcelStyledCell(value, CellStyle.Currency);
}
else if (value is DateTime)
{
//check if there's no time information and use the appropriate style
WriteExcelStyledCell(value, ((DateTime)value).TimeOfDay.CompareTo(new TimeSpan(0, 0, 0, 0, 0)) == 0 ? CellStyle.ShortDate : CellStyle.DateTime);
}
else
{
WriteExcelStyledCell(value, CellStyle.General);
}
}
#region IDisposable Members
public void Dispose()
{
if (_writer == null)
return;
_writer.Close();
_writer = null;
}
#endregion
}
,然後在winform應用程序創建下面的函數...
public static void ExcelExport(DataTable data, String fileName, bool openAfter)
{
//export a DataTable to Excel
DialogResult retry = DialogResult.Retry;
OpenFileDialog openFileDialog = new OpenFileDialog();
while (retry == DialogResult.Retry)
{
try
{
using (ExcelWriter writer = new ExcelWriter(fileName))
{
writer.WriteStartDocument();
// Write the worksheet contents
writer.WriteStartWorksheet("Sheet1");
//Write header row
writer.WriteStartRow();
foreach (DataColumn col in data.Columns)
writer.WriteExcelUnstyledCell(col.Caption);
writer.WriteEndRow();
//write data
foreach (DataRow row in data.Rows)
{
writer.WriteStartRow();
foreach (object o in row.ItemArray)
{
if (Convert.IsDBNull(o))
{
writer.WriteExcelUnstyledCell(String.Empty);
}
else
{
writer.WriteExcelAutoStyledCell(o);
}
}
writer.WriteEndRow();
}
// Close up the document
writer.WriteEndWorksheet();
writer.WriteEndDocument();
writer.Close();
if (openAfter)
{
openFileDialog.FileName = fileName;
//openFileDialog.ShowDialog();
openFileDialog.OpenFile();
}
retry = DialogResult.Cancel;
}
}
catch (Exception myException)
{
retry = MessageBox.Show(myException.Message, "Excel Export", MessageBoxButtons.RetryCancel, MessageBoxIcon.Asterisk);
}
}
}
最後,用它從你期望的事件試試這個/方法...
ExcelExport('YourdDataTable',"NameOfYourFile.xml",true);
1
0
根據@Refracted Paladin的回答,我發現的唯一錯誤是
Xml type 'List of xdt:untypedAtomic' does not support a conversion from Clr type 'Guid' to Clr type 'String'.
這是因爲他的代碼不檢查GUID。 所以我修改了一下他的代碼,以便你也可以檢查我的答案。
Type _Type = o.GetType();
if (_Type.Equals(typeof(System.Guid)))
{
string GuidValueStr = Convert.ToString(o);
writer.WriteExcelAutoStyledCell((object)GuidValueStr);
}
後,我改變了功能
public static void ExcelExport(DataTable data, String fileName)
{
try
{
using (ExcelWriter_StackOverFlow writer = new ExcelWriter_StackOverFlow(fileName))
{
writer.WriteStartDocument();
// Write the worksheet contents
writer.WriteStartWorksheet("Sheet1");
//Write header row
writer.WriteStartRow();
foreach (DataColumn col in data.Columns)
writer.WriteExcelUnstyledCell(col.Caption);
writer.WriteEndRow();
//write data
foreach (DataRow row in data.Rows)
{
writer.WriteStartRow();
foreach (object o in row.ItemArray)
{
if (o.ToString().Length <= 0)
{
writer.WriteExcelAutoStyledCell(string.Empty);
}
else {
Type _Type = o.GetType();
if (_Type.Equals(typeof(System.Guid)))
{
string GuidValueStr = Convert.ToString(o);
writer.WriteExcelAutoStyledCell((object)GuidValueStr);
continue;
}
writer.WriteExcelAutoStyledCell(o);
}
}
writer.WriteEndRow();
}
// Close up the document
writer.WriteEndWorksheet();
writer.WriteEndDocument();
writer.Close();
}
}
catch (Exception myException)
{
throw myException;
}
}
相關問題
- 1. 將2 gridview導入excel
- 2. 將Excel數據導入GridView
- 3. 將GridView導入到Excel
- 4. 將Excel導入Rails應用程序
- 5. 使用asp.net將csv導入到gridview中
- 6. 將excel/CSV文件數據導入GridView winforms中的可見列
- 7. 將Excel導入到matlab中
- 8. 導出GridView到Excel
- 9. GridView導出到Excel
- 10. 將GridView導出到Excel需要將GridView放置在窗體標記中
- 11. 從gridview到excel導出 - 我如何才能在應用程序中看到excel?
- 12. 將excel數據導入到sql server數據庫表中並在gridview中顯示
- 13. 導出一個gridview在c中的excel#
- 14. 警告:將GridView導出到Excel文件
- 15. 將GridView導出到Excel(不工作)
- 16. 如何將GridView導出到Excel?
- 17. 如何使用C#將asp.net中的gridview導出到Excel中?
- 18. 將Excel數據導入Word中用VB
- 19. 將excel導入到rails應用程序中的sqlite中
- 20. 在SQL導入'NULL'行中導入excel
- 21. 使用jquery將excel表格中的數據導出到gridview
- 22. Telerik,如何在這些環境中將GridView導出到Excel?
- 23. 將巨大的Excel文件導入到Rails應用程序中
- 24. 導出的GridView表到Excel
- 25. 導出GridView到excel#2
- 26. 在Excel中導入Json
- 27. 將XML數據導入excel
- 28. 將c#winforms導入excel,
- 29. 將Excel文件導入NSArray
- 30. 將內容導入excel表
你的代碼是簡單的表值工作nice..its ......但我在我的數據庫使用層次....所以其顯示一些錯誤...那麼如何解決這個問題呢? – Suryakavitha
我發現的唯一錯誤是'Xml type'xdt:untypedAtomic列表不支持從Clr類型'Guid'到Clr類型'String'的轉換。「這是因爲他的代碼不檢查GUID。所以我修改了一下他的代碼,這樣你也可以檢查我的答案。 –