请问如果服务器端有没有安装过Office,那么使用C#怎么生成Excel
答案:2 悬赏:30 手机版
解决时间 2021-12-21 17:52
- 提问者网友:心如荒岛囚我终老
- 2021-12-21 02:16
如上
最佳答案
- 五星知识达人网友:时间的尘埃
- 2021-12-21 03:22
用数据流. 先将数据绑定到dataview.然后如下. 注:dgvAgeWeekSex==dataview的name private void SaveAs() //另存新档按钮 导出成Excel { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "Export Excel File To"; saveFileDialog.ShowDialog(); Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string str = ""; try{ //写标题 for (int i = 0; i < dgvAgeWeekSex.ColumnCount; i ) { if (i > 0) { str = "\t";}str = dgvAgeWeekSex.Columns[i].HeaderText; } sw.WriteLine(str); //写内容 for (int j = 0; j < dgvAgeWeekSex.Rows.Count; j ){string tempStr = "";<br/>for (int k = 0; k < dgvAgeWeekSex.Columns.Count; k )<br/>{<br/>if (k > 0){tempStr = "\t";}tempStr = dgvAgeWeekSex.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); } catch (Exception e) {MessageBox.Show(e.ToString());<br/>} finally{ sw.Close(); myStream.Close(); } }
全部回答
- 1楼网友:一袍清酒付
- 2021-12-21 04:29
这个答案应该是对的
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯